<!DOCTYPE html><html lang="zh-CN"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1"><meta name="format-detection" content="telephone=no"><meta name="apple-mobile-web-app-capable" content="yes"><meta name="apple-mobile-web-app-status-bar-style" content="black"><link rel="icon" href="/blog/blog/images/icons/favicon-16x16.png?v=2.6.2" type="image/png" sizes="16x16"><link rel="icon" href="/blog/blog/images/icons/favicon-32x32.png?v=2.6.2" type="image/png" sizes="32x32"><meta property="og:type" content="website">
<meta property="og:title" content="斯人若彩虹 遇上方知有">
<meta property="og:url" content="https://bruce-jj.gitee.io/blog/page/4/index.html">
<meta property="og:site_name" content="斯人若彩虹 遇上方知有">
<meta property="og:locale" content="zh_CN">
<meta property="article:author" content="何水浪">
<meta name="twitter:card" content="summary"><title>斯人若彩虹 遇上方知有</title><link ref="canonical" href="https://bruce-jj.gitee.io/blog/page/4/index.html"><link rel="dns-prefetch" href="https://cdn.jsdelivr.net"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@5.12.1/css/all.min.css" type="text/css"><link rel="stylesheet" href="/blog/css/index.css?v=2.6.2"><script>var Stun = window.Stun || {};
var CONFIG = {
  root: '/blog/',
  algolia: undefined,
  assistSearch: undefined,
  fontIcon: {"prompt":{"success":"fas fa-check-circle","info":"fas fa-arrow-circle-right","warning":"fas fa-exclamation-circle","error":"fas fa-times-circle"},"copyBtn":"fas fa-copy"},
  sidebar: {"offsetTop":"20px","tocMaxDepth":6},
  header: {"enable":true,"showOnPost":true,"scrollDownIcon":false},
  postWidget: {"endText":true},
  nightMode: {"enable":true},
  back2top: {"enable":true},
  codeblock: {"style":"default","highlight":"light","wordWrap":false},
  reward: false,
  fancybox: false,
  zoomImage: {"gapAside":"20px"},
  galleryWaterfall: undefined,
  lazyload: false,
  pjax: undefined,
  externalLink: {"icon":{"enable":true,"name":"fas fa-external-link-alt"}},
  shortcuts: undefined,
  prompt: {"copyButton":"复制","copySuccess":"复制成功","copyError":"复制失败"},
  sourcePath: {"js":"js","css":"css","images":"images"},
};

window.CONFIG = CONFIG;</script><meta name="generator" content="Hexo 5.4.0"><link rel="alternate" href="/blog/atom.xml" title="斯人若彩虹 遇上方知有" type="application/atom+xml">
</head><body><div class="container" id="container"><header class="header" id="header"><div class="header-inner"><nav class="header-nav header-nav--fixed"><div class="header-nav-inner"><div class="header-nav-menubtn"><i class="fas fa-bars"></i></div><div class="header-nav-menu"><div class="header-nav-menu-item"><a class="header-nav-menu-item__link" href="/blog/"><span class="header-nav-menu-item__icon"><i class="fas fa-home"></i></span><span class="header-nav-menu-item__text">首页</span></a></div><div class="header-nav-menu-item"><a class="header-nav-menu-item__link" href="/blog/archives/"><span class="header-nav-menu-item__icon"><i class="fas fa-folder-open"></i></span><span class="header-nav-menu-item__text">归档</span></a></div><div class="header-nav-menu-item"><a class="header-nav-menu-item__link" href="/blog/categories/"><span class="header-nav-menu-item__icon"><i class="fas fa-layer-group"></i></span><span class="header-nav-menu-item__text">分类</span></a></div><div class="header-nav-menu-item"><a class="header-nav-menu-item__link" href="/blog/tags/"><span class="header-nav-menu-item__icon"><i class="fas fa-tags"></i></span><span class="header-nav-menu-item__text">标签</span></a></div><div class="header-nav-menu-item"><a class="header-nav-menu-item__link" href="/blog/friends/"><span class="header-nav-menu-item__icon"><i class="fas fa-link"></i></span><span class="header-nav-menu-item__text">友链</span></a></div></div><div class="header-nav-search"><span class="header-nav-search__icon"><i class="fas fa-search"></i></span><span class="header-nav-search__text">搜索</span></div><div class="header-nav-mode"><div class="mode"><div class="mode-track"><span class="mode-track-moon"></span><span class="mode-track-sun"></span></div><div class="mode-thumb"></div></div></div></div></nav><div class="header-banner"><div class="header-banner-info"><div class="header-banner-info__title">斯人若彩虹 遇上方知有</div><div class="header-banner-info__subtitle"></div></div></div></div></header><main class="main" id="main"><div class="main-inner"><div class="content-wrap" id="content-wrap"><div class="content content-home" id="content"><section class="postlist"><article class="postlist-item post"><header class="post-header"><h1 class="post-title"><a class="post-title__link" href="/blog/2021/03/16/%E7%BF%BB%E8%AF%91%E4%BD%9C%E5%93%81-%E5%A4%A7%E6%95%B0%E6%8D%AE/">翻译作品-大数据</a></h1><div class="post-meta"><span class="post-meta-item post-meta-item--createtime"><span class="post-meta-item__icon"><i class="far fa-calendar-plus"></i></span><span class="post-meta-item__info">发表于</span><span class="post-meta-item__value">2021-03-16</span></span><span class="post-meta-item post-meta-item--updatetime"><span class="post-meta-item__icon"><i class="far fa-calendar-check"></i></span><span class="post-meta-item__info">更新于</span><span class="post-meta-item__value">2021-05-04</span></span><span class="post-meta-item post-meta-item--wordcount"><span class="post-meta-item__icon"><i class="far fa-file-word"></i></span><span class="post-meta-item__info">字数统计</span><span class="post-meta-item__value">341</span></span><span class="post-meta-item post-meta-item--readtime"><span class="post-meta-item__icon"><i class="far fa-clock"></i></span><span class="post-meta-item__info">阅读时长</span><span class="post-meta-item__value">3分</span></span></div></header><div class="post-body"><div class="post-excerpt"><!-- 页面必备样式 -->
<style>
.back-top{position: fixed;top: 500px;height: 0px;float: right;right: 50px;}
.back-top>a{font-weight:bold;width: 50px;height: 50px;text-align: center;text-decoration: none;font-size: 5px;line-height: 50px;border-radius: 50px;background-color:rgba(255,255,255,0.5);}
</style>
<div class="back-top">
    <a href="#top"> >返回顶部< </a>
</div>
<!-- toc -->
<!-- 必备样式结束 -->



        <h2 id="Big-Data-In-5-Minutes-What-Is-Big-Data-5分钟大数据-什么是大数据"   >
          <a href="#Big-Data-In-5-Minutes-What-Is-Big-Data-5分钟大数据-什么是大数据" class="heading-link"><i class="fas fa-link"></i></a><a href="#Big-Data-In-5-Minutes-What-Is-Big-Data-5分钟大数据-什么是大数据" class="headerlink" title="Big Data In 5 Minutes | What Is Big Data?| 5分钟大数据|什么是大数据"></a>Big Data In 5 Minutes | What Is Big Data?| 5分钟大数据|什么是大数据</h2>
      
        <h2 id="Introduction-To-Big-Data-Big-Data-Explained-Simplilearn-大数据入门"   >
          <a href="#Introduction-To-Big-Data-Big-Data-Explained-Simplilearn-大数据入门" class="heading-link"><i class="fas fa-link"></i></a><a href="#Introduction-To-Big-Data-Big-Data-Explained-Simplilearn-大数据入门" class="headerlink" title="Introduction To Big Data |Big Data Explained |Simplilearn 大数据入门"></a>Introduction To Big Data |Big Data Explained |Simplilearn 大数据入门</h2>
      <p>This video will help you understand what Big Data is, the 5V’s of Big Data, why Hadoop came into existence, and what Hadoop is. You will also learn about the storage unit and processing unit of Hadoop, and the implementation of Big Data through use cases. In the end, we will have a quiz on Hadoop. A massive amount of data that cannot be stored, processed, and analyzed using the traditional ways is known as Big Data. Hadoop is a framework that manages Big Data storage in a distributed way and processes it parallelly. Now, let’s deep dive into this video and understand Big Data and Hadoop. </p>
<p>本视频将帮助您理解什么是大数据，大数据的5V, Hadoop为什么会出现，以及Hadoop是什么。通过用例了解Hadoop的存储单元和处理单元，以及大数据的实现。最后，我们会有一个关于Hadoop的测试。大量的数据无法通过传统的方式进行存储、处理和分析，这就是所谓的大数据。Hadoop是一个以分布式方式管理大数据存储并并行处理的框架。现在，让我们深入研究这个视频，了解大数据和Hadoop。</p>
<div style="position: relative; padding: 30% 45%;">
<iframe style="position: absolute; width: 100%; height: 100%; left: 0; top: 0;" 
src="https://player.bilibili.com/player.html?aid=756891724&bvid=BV1Wr4y1A7MX&cid=305097153&page=1"
 frameborder="no" scrolling="no"></iframe>
</div></div></div></article><article class="postlist-item post"><header class="post-header"><h1 class="post-title"><a class="post-title__link" href="/blog/2021/03/16/Spring/">Spring学习笔记(所有代码FROM SIKI学院)</a></h1><div class="post-meta"><span class="post-meta-item post-meta-item--createtime"><span class="post-meta-item__icon"><i class="far fa-calendar-plus"></i></span><span class="post-meta-item__info">发表于</span><span class="post-meta-item__value">2021-03-16</span></span><span class="post-meta-item post-meta-item--updatetime"><span class="post-meta-item__icon"><i class="far fa-calendar-check"></i></span><span class="post-meta-item__info">更新于</span><span class="post-meta-item__value">2021-03-17</span></span><span class="post-meta-item post-meta-item--wordcount"><span class="post-meta-item__icon"><i class="far fa-file-word"></i></span><span class="post-meta-item__info">字数统计</span><span class="post-meta-item__value">5.5k</span></span><span class="post-meta-item post-meta-item--readtime"><span class="post-meta-item__icon"><i class="far fa-clock"></i></span><span class="post-meta-item__info">阅读时长</span><span class="post-meta-item__value">45分</span></span></div></header><div class="post-body"><div class="post-excerpt"><!-- 页面必备样式 -->
<style>
.back-top{position: fixed;top: 500px;height: 0px;float: right;right: 50px;}
.back-top>a{font-weight:bold;width: 50px;height: 50px;text-align: center;text-decoration: none;font-size: 5px;line-height: 50px;border-radius: 50px;background-color:rgba(255,255,255,0.5);}
</style>
<div class="back-top">
    <a href="#top"> >返回顶部< </a>
</div>
<!-- toc -->
<!-- 必备样式结束 -->


        <h1 id="SSM框架系列之二-大管家Spring"   >
          <a href="#SSM框架系列之二-大管家Spring" class="heading-link"><i class="fas fa-link"></i></a><a href="#SSM框架系列之二-大管家Spring" class="headerlink" title="SSM框架系列之二 大管家Spring"></a>SSM框架系列之二 大管家Spring</h1>
      
        <h2 id="Spring是什么？"   >
          <a href="#Spring是什么？" class="heading-link"><i class="fas fa-link"></i></a><a href="#Spring是什么？" class="headerlink" title="Spring是什么？"></a>Spring是什么？</h2>
      <p>Spring是于2003 年兴起的一个轻量级的Java 开源框架，它由Rod Johnson创建。传统J2EE应用的开发效率低，Spring作为开源的中间件，提供J2EE应用的各层的解决方案，Spring贯穿了表现层、业务层及持久层，而不是仅仅专注于某一层的方案。可以说Spring是企业应用开发的“一站式（full-stack）”选择。然而，Spring并不想取代那些已有的框架，而是与它们无缝地整合。<br>简单来说，Spring是一个轻量级控制反转(IoC)和面向切面(AOP)的容器框架。</p>
<p>为什么要使用Spring？</p>
<ol>
<li><p>方便解耦，简化开发<br>通过Spring提供的IoC容器，我们可以将对象之间的依赖关系交由Spring进行控制，避免硬编码所造成的过度程序耦合。有了Spring，用户不必再为单实例模式类、属性文件解析等这些很底层的需求编写代码，可以更专注于上层的应用。</p>
</li>
<li><p>AOP编程的支持<br>通过Spring提供的AOP功能，方便进行面向切面的编程，许多不容易用传统OOP实现的功能可以通过AOP轻松应付。</p>
</li>
<li><p>声明式事务的支持<br>在Spring中，我们可以从单调烦闷的事务管理代码中解脱出来，通过声明式方式灵活地进行事务的管理，提高开发效率和质量。</p>
</li>
<li><p>方便程序的测试<br>可以用非容器依赖的编程方式进行几乎所有的测试工作，在Spring里，测试不再是昂贵的操作，而是随手可做的事情。例如：Spring对Junit4支持，可以通过注解方便的测试Spring程序。</p>
</li>
<li><p>方便集成各种优秀框架<br>Spring不排斥各种优秀的开源框架，相反，Spring可以降低各种框架的使用难度，Spring提供了对各种优秀框架（如Struts、Hibernate、MyBatis 、Hessian、Quartz）等的直接支持。</p>
</li>
<li><p>降低Java EE API的使用难度<br>Spring对很多难用的Java EE API（如JDBC，JavaMail，远程调用等）提供了一个薄薄的封装层，通过Spring的简易封装，这些Java EE API的使用难度大为降低。</p>
</li>
</ol>

        <h2 id="我能从这门课中学到什么？"   >
          <a href="#我能从这门课中学到什么？" class="heading-link"><i class="fas fa-link"></i></a><a href="#我能从这门课中学到什么？" class="headerlink" title="我能从这门课中学到什么？"></a>我能从这门课中学到什么？</h2>
      <ol>
<li>了解Spring框架、IOC、DI的概念；</li>
<li>掌握Spring框架搭建、配置与使用；</li>
<li>掌握Spring容器创建与对象管理；</li>
<li>掌握Spring xml配置式开发与注解开发；</li>
<li>掌握Spring AOP相关知识；</li>
<li>掌握SpringJDBC相关知识；</li>
<li>将Spring集成项目中进行测试；</li>
</ol>

        <h1 id="开始学习Spring框架"   >
          <a href="#开始学习Spring框架" class="heading-link"><i class="fas fa-link"></i></a><a href="#开始学习Spring框架" class="headerlink" title="开始学习Spring框架"></a>开始学习Spring框架</h1>
      
        <h2 id="Part-1-Spring框架介绍与入门案例："   >
          <a href="#Part-1-Spring框架介绍与入门案例：" class="heading-link"><i class="fas fa-link"></i></a><a href="#Part-1-Spring框架介绍与入门案例：" class="headerlink" title="Part.1 Spring框架介绍与入门案例："></a>Part.1 Spring框架介绍与入门案例：</h2>
      <p>1、搭建Spring HelloWorld项目<br>    a)Spring下载：<span class="exturl"><a class="exturl__link"   target="_blank" rel="noopener" href="http://repo.spring.io/release/org/springframework/spring/" >http://repo.spring.io/release/org/springframework/spring/</a><span class="exturl__icon"><i class="fas fa-external-link-alt"></i></span></span><br>    b)导包：（以下5个包称为基本包，根据你使用的版本可能还需导入log4j包，咱们学习的版本暂时无需导入）<br>        i.核心包：bean、context、core、expression；<br>        ii.日志包：apache.commons.logging<br>    c)引入约束：bean约束和引入主配置文件头；<br>    d)测试：将自定义对象由自己创建交由Spring管理；</p>
<p>2、SpringIDE - sts插件安装；<br>    a)下载地址：<span class="exturl"><a class="exturl__link"   target="_blank" rel="noopener" href="http://spring.io/tools/sts/all/" >http://spring.io/tools/sts/all/</a><span class="exturl__icon"><i class="fas fa-external-link-alt"></i></span></span><br>    b)安装springIDE；<br>        Spring-IoC | DI概念介绍：（要实现IOC依赖DI的支持）<br>        IoC（Inversion of Control）：反转控制：将我们自己创建对象的工作交给Spring容器帮我们完成；<br>        DI（Dependency Injection）：依赖注入：将值通过配置的方式为变量初始化/赋值(注入)；<br>    c)注入方式：set方法注入、构造注入、字段注入(不推荐)；<br>    d)注入类型：<br>        i.值类型(value) – 八大基本数据类型；<br>        ii.引用类型(ref) – String、自定义对象；</p>

        <h2 id="Part-2-Spring配置："   >
          <a href="#Part-2-Spring配置：" class="heading-link"><i class="fas fa-link"></i></a><a href="#Part-2-Spring配置：" class="headerlink" title="Part.2 Spring配置："></a>Part.2 Spring配置：</h2>
      <p>1、Xml配置<br>a)Bean元素：交由Spring管理的对象都要配置在bean标签中；<br>i.Bean标签介绍和创建方式：空参构造、静态工厂、动态工厂；<br>ii.Scope属性介绍：singleton、protoptype、request、session；<br>iii.初始化方法Init-method和 销毁方法destroy-method介绍；<br>b)属性注入：<br>i.Set方法注入；<br>ii.构造函数注入；<br>iii.复杂类型注入：Array、List、Set、Map、Properties</p>
<p>2、Spring注解配置；<br>a)导包和约束：基本包、aop包+context约束；<br>b)将对象注册到容器内；<br>c)用注解配置Scope属性；<br>d)注解配置init-method与destroy-method；<br>e)注解配置属性注入，值类型与引用类型； </p>
<p>3、Spring &amp; JUnit进行测试：<br>a)导包： test包（依赖 aop包）；<br>b)使用@RunWith注解创建spring容器；<br>c)使用@ContextConfiguration读取spring配置文件；<br>4、主配置文件的分包配置；</p>

        <h2 id="Part-3-小结与练习：将Spring应用到servlet项目中"   >
          <a href="#Part-3-小结与练习：将Spring应用到servlet项目中" class="heading-link"><i class="fas fa-link"></i></a><a href="#Part-3-小结与练习：将Spring应用到servlet项目中" class="headerlink" title="Part.3 小结与练习：将Spring应用到servlet项目中"></a>Part.3 小结与练习：将Spring应用到servlet项目中</h2>
      <p>1、使用servlet技术开发用户登陆功能；<br>2、在项目中加入Spring框架<br>i.将service、dao、dateSource交给Spring管理；<br>ii.在web.xml中配置Spring监听器和读取配置文件；</p>

        <h2 id="Part-4-Spring-aop："   >
          <a href="#Part-4-Spring-aop：" class="heading-link"><i class="fas fa-link"></i></a><a href="#Part-4-Spring-aop：" class="headerlink" title="Part.4 Spring-aop："></a>Part.4 Spring-aop：</h2>
      <p>1、aop思想介绍（面向切面编程）：将纵向重复代码，横向抽取解决，简称：横切<br>2、Spring中的aop：无需我们自己写动态代理的代码，spring可以将容器中管理对象生成动态代理对象，前提是我们对他进行一些设置；<br>3、Spring-aop是基于动态代理的 – 优先选用JDKProxy动态代理；<br>a)Proxy动态代理：被代理的对象必须要实现接口；<br>b)Cglib动态代理：被代理的对象不能被final修饰，基于继承；<br>4、Spring aop相关名词说明 – 详见 表-[Spring术语解释]；<br>5、Spring aop配置：<br>a)导包：<br>i.基本包；<br>ii.spring-aspects和spring-aop ；<br>iii.aop联盟包 – aopalliance；<br>iv.aop织入包 - aspectj.weaver；<br>b)自定义通知，五种自定义通知类型：<br>i.before 前置通知<br>ii.after 最终通知（后置通知）<br>iii.afterReturning 成功通知（后置通知）<br>iv.afterThrowing 异常通知（后置通知）<br>v.around 环绕通知<br>c)配置applicationContext.xml：</p>

        <h2 id="Part-5-Spring与Jdbc"   >
          <a href="#Part-5-Spring与Jdbc" class="heading-link"><i class="fas fa-link"></i></a><a href="#Part-5-Spring与Jdbc" class="headerlink" title="Part.5 Spring与Jdbc"></a>Part.5 Spring与Jdbc</h2>
      <p>1、使用JdbcTemplate操作数据库；<br>a)导包：新增jdbc、tx<br>2、JdbcTemplate的增删改查操作；<br>3、让Spring容器管理JdbcTemplate；<br>4、继承JdbcDaoSupport甩开JdbcTemplate；<br>5、在Spring中读取配置文件；</p>

        <h2 id="Part-6-Spring中的aop事务"   >
          <a href="#Part-6-Spring中的aop事务" class="heading-link"><i class="fas fa-link"></i></a><a href="#Part-6-Spring中的aop事务" class="headerlink" title="Part.6 Spring中的aop事务"></a>Part.6 Spring中的aop事务</h2>
      <p>1、事务相关知识：<br>a)什么是事务：把多条数据库操作捆绑到一起执行，要么都成功，要么都失败；<br>b)事务的原则ACID：<br>i.原子性：事务包含的所有操作，要么全部成功，要么全部失败回滚，成功全部应用到数据库，失败不能对数据库有任何影响；<br>ii.一致性：事务在执行前和执行后必须一致；例如A和B一共有100块钱，无论A、B之间如何转账，他们的钱始终相加都是100；<br>iii.隔离性：多用户并发访问同一张表时，数据库为每一个用户开启新的事务，该事务不能被其他事务所影响，相互有隔离；<br>iv.持久性：一个事务一旦提交，则对数据库中数据的改变是永久的，即便系统故障也不会丢失；<br>c)并发可能引起的问题：<br>i.脏读：一个事务读取到另一个事务未提交的数据；<br>ii.不可重复读：一个事务读取到另一个事务已提交(Update操作)的数据，导致前后读取不一致；<br>iii.幻读（虚读）：一个事务中读取到别的事务插入(Insert操作)的数据，导致前后读取不一致；<br>d)事务的隔离级别：根据实际情况选择；<br>i.Serializable串行化：可避免脏读、不可重复读和幻读；<br>ii.Repeatable read可重复读：可避免脏读、不可重复读；（MySql默认值）<br>iii.Read committed读已提交：可避免脏读；<br>iv.Read uncommitted读未提交：任何情况都无法保证；<br>2、Spring-aop事务-搭建环境；<br>a)事务基本操作：打开事务、提交事务、回滚事务；<br>b)Spring中利用接口来管理不同框架的事务操作；<br>i.通过实现PlatformTransactionManager接口支持不同的框架完成各自的事务处理；<br>ii.为不同平台提供对应的事务管理器的实现：<br>JDBC&amp;Mybatis：DataSourceTransactionManager；<br>…<br>c)Spring-aop事务通过配置事务的隔离级别、事务传播行为、是否只读来操作；<br>i.隔离级别：串行化、可重复读、读已提交、读未提交；<br>ii.是否只读：<br>1.true：不可改变数据库中的数据，查询操作推荐，<br>2.false：可以改变数据库数据；<br>iii.事务传播行为：事务方法嵌套调用的规则：<br>xService.x();    -&gt;    yService.y();<br>1.REQUIRED：如果当前没有事务，就创建一个新事务，如果当前存在事务，就加入该事务，该设置是最常用的设置；<br>2.REQUIRES_NEW：创建新事务，无论当前存不存在事务，都创建新事务；<br>3.SUPPORTS：支持当前事务，如果当前存在事务，就加入该事务，如果当前不存在事务，就以非事务执行；<br>4.NOT_SUPPORTED：以非事务方式执行操作，如果当前存在事务，就把当前事务挂起（暂停）；<br>5.MANDATORY：支持当前事务，如果当前存在事务，就加入该事务，如果当前不存在事务，就抛出异常；<br>6.NEVER：以非事务方式执行，如果当前存在事务，则抛出异常；<br>7.NESTED：如果当前存在事务，则在嵌套事务内执行。如果当前没有事务，则执行与REQUIRED类似的操作。</p>
<p>3、Spring-aop事务 – 从麻烦的事务代码中走出之xml配置版aop事务；<br>a)使用经典的转账案例进行测试，准备数据：bean、service、dao；<br>b)使用事务需要额外导入tx包和tx约束；<br>c)配置事务核心管理器: DataSourceTransactionManager；<br>d)配置事务通知 tx:Advice；<br>e)配置aop；<br>4、Spring-aop事务 – 从麻烦的事务代码中走出之注解版aop事务。</p>

        <h1 id="Part-7-Spring整合Mybatis加入事务操作数据库"   >
          <a href="#Part-7-Spring整合Mybatis加入事务操作数据库" class="heading-link"><i class="fas fa-link"></i></a><a href="#Part-7-Spring整合Mybatis加入事务操作数据库" class="headerlink" title="Part.7 Spring整合Mybatis加入事务操作数据库"></a>Part.7 Spring整合Mybatis加入事务操作数据库</h1>
      <p>1、整合Mybatis<br>a)导包：<br>i.Spring：基本包、aop、aspects、jdbc、tx、test；<br>ii.Mybatis：mybatis-3.4.6<br>iii.整合包：mybatis-spring-1.3.2<br>iv.三方包：<br>1.aopalliance<br>2.aspectj.weaver<br>3.c3p0-0.9.5.2<br>4.mchange-commons-java-0.2.11<br>5.mysql-connector-java-5.1.46-bin<br>6.ojdbc7<br>b)创建项目结构(package)：bean、service、mapper、test；<br>c)创建配置文件：sqlMapperConfig、applicaitonContext<br>2、创建测试用例：使用Mapper扫描开发，转账；<br>3、在service中加入事务：利用Spring-aop事务解决转账异常问题；</p>

        <h1 id="Spring实例一-纯Spring"   >
          <a href="#Spring实例一-纯Spring" class="heading-link"><i class="fas fa-link"></i></a><a href="#Spring实例一-纯Spring" class="headerlink" title="Spring实例一 纯Spring"></a>Spring实例一 纯Spring</h1>
      
        <h2 id="applicationContext-xml"   >
          <a href="#applicationContext-xml" class="heading-link"><i class="fas fa-link"></i></a><a href="#applicationContext-xml" class="headerlink" title="applicationContext.xml"></a>applicationContext.xml</h2>
      <figure class="highlight xml"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">beans</span> <span class="attr">xmlns:xsi</span>=<span class="string">&quot;http://www.w3.org/2001/XMLSchema-instance&quot;</span> </span></span><br><span class="line"><span class="tag"><span class="attr">xmlns</span>=<span class="string">&quot;http://www.springframework.org/schema/beans&quot;</span> </span></span><br><span class="line"><span class="tag"><span class="attr">xsi:schemaLocation</span>=<span class="string">&quot;http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd &quot;</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="comment">&lt;!-- name 是起一个名字，我们可以通过这个name来利用容器获取对象 </span></span><br><span class="line"><span class="comment">	name 可以使用特殊字符</span></span><br><span class="line"><span class="comment">	name 可以重复 </span></span><br><span class="line"><span class="comment">	我们在实际开发中不推荐将多个对象名字命名为重复的--&gt;</span></span><br><span class="line">	</span><br><span class="line"><span class="comment">&lt;!-- id与name作用基本相同，单不推荐使用 不支持特殊字符，不能重复 --&gt;</span></span><br><span class="line"><span class="comment">&lt;!-- class：是被管理对象的全包名，spring会通过这个包名来创建对象 --&gt;</span></span><br><span class="line"><span class="comment">&lt;!-- request 在web环境下，如果scope属性为request 那么这个对象被创建出来 他的生命周期会与request请求一致--&gt;</span></span><br><span class="line"><span class="comment">&lt;!-- session 同理 ，生命周期与session一致 --&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;<span class="name">bean</span> <span class="attr">name</span>=<span class="string">&quot;user&quot;</span> <span class="attr">class</span>=<span class="string">&quot;com.sikiedu.bean.User&quot;</span> <span class="attr">lazy-init</span>=<span class="string">&quot;default&quot;</span> <span class="attr">scope</span>=<span class="string">&quot;prototype&quot;</span> <span class="attr">init-method</span>=<span class="string">&quot;userInit&quot;</span> <span class="attr">destroy-method</span>=<span class="string">&quot;userDestroy&quot;</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">&quot;u_id&quot;</span> <span class="attr">value</span>=<span class="string">&quot;2&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">property</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">bean</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="comment">&lt;!-- 导入其他Spring的配置文件 --&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">import</span> <span class="attr">resource</span>=<span class="string">&quot;/applicationContext_Injection.xml&quot;</span>/&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;/<span class="name">beans</span>&gt;</span></span><br></pre></td></tr></table></div></figure>


        <h2 id="User-Bean对象"   >
          <a href="#User-Bean对象" class="heading-link"><i class="fas fa-link"></i></a><a href="#User-Bean对象" class="headerlink" title="User Bean对象"></a>User Bean对象</h2>
      <figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">User</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">	<span class="keyword">private</span> Integer u_id;</span><br><span class="line">	<span class="keyword">private</span> String u_username;</span><br><span class="line">	<span class="keyword">private</span> String u_password;</span><br><span class="line"></span><br><span class="line">	</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="title">User</span><span class="params">(String u_username, Pet u_pet)</span> </span>&#123;</span><br><span class="line">		System.out.println(<span class="string">&quot;方法1 String, Pet&quot;</span>);</span><br><span class="line">		<span class="keyword">this</span>.u_username = u_username;</span><br><span class="line">		<span class="keyword">this</span>.u_pet = u_pet;</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="title">User</span><span class="params">(Integer u_username, Pet u_pet)</span> </span>&#123;</span><br><span class="line">		System.out.println(<span class="string">&quot;方法2 Integer, Pet&quot;</span>);</span><br><span class="line">		<span class="keyword">this</span>.u_username = u_username.toString();</span><br><span class="line">		<span class="keyword">this</span>.u_pet = u_pet;</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="title">User</span><span class="params">(Pet u_pet, Integer u_username)</span> </span>&#123;</span><br><span class="line">		System.out.println(<span class="string">&quot;方法3 Pet, Integer &quot;</span>);</span><br><span class="line">		<span class="keyword">this</span>.u_username = u_username.toString();</span><br><span class="line">		<span class="keyword">this</span>.u_pet = u_pet;</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	<span class="comment">//加入宠物字段</span></span><br><span class="line">	<span class="keyword">private</span> Pet u_pet;</span><br><span class="line">	</span><br><span class="line">	<span class="function"><span class="keyword">public</span> Pet <span class="title">getU_pet</span><span class="params">()</span> </span>&#123;</span><br><span class="line">		<span class="keyword">return</span> u_pet;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setU_pet</span><span class="params">(Pet u_pet)</span> </span>&#123;</span><br><span class="line">		<span class="keyword">this</span>.u_pet = u_pet;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="title">User</span><span class="params">()</span> </span>&#123;</span><br><span class="line">		System.out.println(<span class="string">&quot;User 对象空参构造方法&quot;</span>);</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="function"><span class="keyword">public</span> Integer <span class="title">getU_id</span><span class="params">()</span> </span>&#123;</span><br><span class="line">		<span class="keyword">return</span> u_id;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setU_id</span><span class="params">(Integer u_id)</span> </span>&#123;</span><br><span class="line">		<span class="keyword">this</span>.u_id = u_id;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="function"><span class="keyword">public</span> String <span class="title">getU_username</span><span class="params">()</span> </span>&#123;</span><br><span class="line">		<span class="keyword">return</span> u_username;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setU_username</span><span class="params">(String u_username)</span> </span>&#123;</span><br><span class="line">		<span class="keyword">this</span>.u_username = u_username;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="function"><span class="keyword">public</span> String <span class="title">getU_password</span><span class="params">()</span> </span>&#123;</span><br><span class="line">		<span class="keyword">return</span> u_password;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setU_password</span><span class="params">(String u_password)</span> </span>&#123;</span><br><span class="line">		<span class="keyword">this</span>.u_password = u_password;</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">userInit</span><span class="params">()</span> </span>&#123;</span><br><span class="line">		System.out.println(<span class="string">&quot;user init &quot;</span>);</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">userDestroy</span><span class="params">()</span> </span>&#123;</span><br><span class="line">		System.out.println(<span class="string">&quot;user destroy&quot;</span>);</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	<span class="meta">@Override</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> String <span class="title">toString</span><span class="params">()</span> </span>&#123;</span><br><span class="line">		<span class="keyword">return</span> <span class="string">&quot;User [u_id=&quot;</span> + u_id + <span class="string">&quot;, u_username=&quot;</span> + u_username + <span class="string">&quot;, u_password=&quot;</span> + u_password + <span class="string">&quot;, u_pet=&quot;</span> + u_pet</span><br><span class="line">				+ <span class="string">&quot;]&quot;</span>;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>


        <h2 id="Test-测试方法主函数"   >
          <a href="#Test-测试方法主函数" class="heading-link"><i class="fas fa-link"></i></a><a href="#Test-测试方法主函数" class="headerlink" title="Test 测试方法主函数"></a>Test 测试方法主函数</h2>
      <figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.sikiedu.test;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> org.junit.Test;</span><br><span class="line"><span class="keyword">import</span> org.springframework.context.ApplicationContext;</span><br><span class="line"><span class="keyword">import</span> org.springframework.context.support.ClassPathXmlApplicationContext;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> com.sikiedu.bean.User;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">HelloSpring</span> </span>&#123;</span><br><span class="line">	</span><br><span class="line">	<span class="comment">//在之前项目中 new 对象是我们自己做的</span></span><br><span class="line">	<span class="meta">@Test</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">Test1</span><span class="params">()</span> </span>&#123;</span><br><span class="line">		<span class="comment">//User u = new User();</span></span><br><span class="line">		<span class="comment">//u.setU_id(1);</span></span><br><span class="line">		<span class="comment">//System.out.println(u);</span></span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	</span><br><span class="line">	<span class="comment">//IOC的反转：创建对象这份工作由我们自己执行反转给spring帮我们执行；</span></span><br><span class="line">	<span class="comment">//IOC的控制：就是由spring帮我们负责创建销毁对象，掌控对象的生命周期等，我们在需要使用对象的时候跟Spring申请即可；</span></span><br><span class="line">	<span class="comment">//IOC是一种编程思想，也是一种新的设计模式，它需要DI（依赖注入）技术的支持；</span></span><br><span class="line">	</span><br><span class="line">	<span class="comment">//spring是一个容器，它将帮我们管理对象</span></span><br><span class="line">	<span class="meta">@Test</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">Test2</span><span class="params">()</span> </span>&#123;</span><br><span class="line">		</span><br><span class="line">		<span class="comment">//根据spring配置文件获取容器对象</span></span><br><span class="line">		<span class="comment">//ApplicationContext 配置的所有bean都会在容器创建的时候被创建出来</span></span><br><span class="line">		<span class="comment">//如果配置的bean较多，那么在创建容的时候，会产生内存过大的问题；这种情况在机器硬件性能较为落后的时候体现的比较明显；</span></span><br><span class="line">		<span class="comment">//延迟加载 true就是创建容器时不加载配置的bean对象，在获取的时候才创建；</span></span><br><span class="line">		ApplicationContext ac = <span class="keyword">new</span> ClassPathXmlApplicationContext(<span class="string">&quot;applicationContext.xml&quot;</span>);</span><br><span class="line">		</span><br><span class="line">		<span class="comment">//通过getBean获取配置好的user对象（程序员向spring容器要对象）</span></span><br><span class="line">		<span class="comment">//User u = (User) ac.getBean(&quot;user&quot;);</span></span><br><span class="line">		User u = ac.getBean(User.class);</span><br><span class="line">		<span class="comment">//System.out.println(u);</span></span><br><span class="line">		</span><br><span class="line">		</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>



        <h1 id="Spring实例二-Spring-Mybatis"   >
          <a href="#Spring实例二-Spring-Mybatis" class="heading-link"><i class="fas fa-link"></i></a><a href="#Spring实例二-Spring-Mybatis" class="headerlink" title="Spring实例二 Spring+Mybatis"></a>Spring实例二 Spring+Mybatis</h1>
      
        <h2 id="Bean对象-Account-java"   >
          <a href="#Bean对象-Account-java" class="heading-link"><i class="fas fa-link"></i></a><a href="#Bean对象-Account-java" class="headerlink" title="Bean对象 Account.java"></a>Bean对象 Account.java</h2>
      <figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Account</span> </span>&#123;</span><br><span class="line">	<span class="keyword">private</span> Integer id;</span><br><span class="line">	<span class="keyword">private</span> String name;</span><br><span class="line">	<span class="keyword">private</span> Double money;</span><br><span class="line">	</span><br><span class="line">	...忽略set，get，toString方法</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>

        <h2 id="Mapper-java文件以及Mapper-xml文件"   >
          <a href="#Mapper-java文件以及Mapper-xml文件" class="heading-link"><i class="fas fa-link"></i></a><a href="#Mapper-java文件以及Mapper-xml文件" class="headerlink" title="Mapper.java文件以及Mapper.xml文件"></a>Mapper.java文件以及Mapper.xml文件</h2>
      
        <h3 id="AcountMapper-java"   >
          <a href="#AcountMapper-java" class="heading-link"><i class="fas fa-link"></i></a><a href="#AcountMapper-java" class="headerlink" title="AcountMapper.java"></a>AcountMapper.java</h3>
      <figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">interface</span> <span class="title">AccountMapper</span> </span>&#123;</span><br><span class="line">	<span class="comment">//操作数据库扣款和加款</span></span><br><span class="line">	</span><br><span class="line">	<span class="comment">//扣款</span></span><br><span class="line">	<span class="function"><span class="keyword">void</span> <span class="title">subMoney</span><span class="params">(Account pay)</span></span>;</span><br><span class="line">	</span><br><span class="line">	</span><br><span class="line">	<span class="comment">//加款</span></span><br><span class="line">	<span class="function"><span class="keyword">void</span> <span class="title">addMoney</span><span class="params">(Account collect)</span></span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></div></figure>


        <h3 id="AcountMapper-xml"   >
          <a href="#AcountMapper-xml" class="heading-link"><i class="fas fa-link"></i></a><a href="#AcountMapper-xml" class="headerlink" title="AcountMapper.xml"></a>AcountMapper.xml</h3>
      <figure class="highlight xml"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; ?&gt;</span></span><br><span class="line"><span class="meta">&lt;!DOCTYPE <span class="meta-keyword">mapper</span></span></span><br><span class="line"><span class="meta">  <span class="meta-keyword">PUBLIC</span> <span class="meta-string">&quot;-//mybatis.org//DTD Mapper 3.0//EN&quot;</span></span></span><br><span class="line"><span class="meta">  <span class="meta-string">&quot;http://mybatis.org/dtd/mybatis-3-mapper.dtd&quot;</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">mapper</span> <span class="attr">namespace</span>=<span class="string">&quot;com.sikiedu.mapper.AccountMapper&quot;</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">update</span> <span class="attr">id</span>=<span class="string">&quot;subMoney&quot;</span> <span class="attr">parameterType</span>=<span class="string">&quot;Account&quot;</span>&gt;</span></span><br><span class="line">		update account set money = money - #&#123;tranferMoney&#125; where id = #&#123;id&#125;</span><br><span class="line">	<span class="tag">&lt;/<span class="name">update</span>&gt;</span></span><br><span class="line">	</span><br><span class="line">	<span class="tag">&lt;<span class="name">update</span> <span class="attr">id</span>=<span class="string">&quot;addMoney&quot;</span> <span class="attr">parameterType</span>=<span class="string">&quot;Account&quot;</span>&gt;</span></span><br><span class="line">		update account set money = money + #&#123;tranferMoney&#125; where id = #&#123;id&#125;</span><br><span class="line">	<span class="tag">&lt;/<span class="name">update</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">mapper</span>&gt;</span></span><br></pre></td></tr></table></div></figure>

        <h2 id="Service层以及实现Impl类"   >
          <a href="#Service层以及实现Impl类" class="heading-link"><i class="fas fa-link"></i></a><a href="#Service层以及实现Impl类" class="headerlink" title="Service层以及实现Impl类"></a>Service层以及实现Impl类</h2>
      
        <h3 id="AccountService-java"   >
          <a href="#AccountService-java" class="heading-link"><i class="fas fa-link"></i></a><a href="#AccountService-java" class="headerlink" title="AccountService.java"></a>AccountService.java</h3>
      <figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">interface</span> <span class="title">AccountService</span> </span>&#123;</span><br><span class="line">	</span><br><span class="line">	<span class="comment">//转账方法</span></span><br><span class="line">	<span class="function"><span class="keyword">void</span> <span class="title">updateTranferAccount</span><span class="params">()</span></span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></div></figure>


        <h3 id="AccountServiceImpl-java"   >
          <a href="#AccountServiceImpl-java" class="heading-link"><i class="fas fa-link"></i></a><a href="#AccountServiceImpl-java" class="headerlink" title="AccountServiceImpl.java"></a>AccountServiceImpl.java</h3>
      <figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.sikiedu.service;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> javax.annotation.Resource;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> com.sikiedu.bean.Account;</span><br><span class="line"><span class="keyword">import</span> com.sikiedu.mapper.AccountMapper;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">AccountServiceImpl</span> <span class="keyword">implements</span> <span class="title">AccountService</span> </span>&#123;</span><br><span class="line">	</span><br><span class="line">	<span class="meta">@Resource(type=AccountMapper.class)</span></span><br><span class="line">	<span class="keyword">private</span> AccountMapper mapper;</span><br><span class="line">	</span><br><span class="line">	<span class="meta">@Override</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">updateTranferAccount</span><span class="params">()</span> </span>&#123;</span><br><span class="line">		</span><br><span class="line">		Double tranferMoney = <span class="number">1000d</span>;</span><br><span class="line">		Account pay = <span class="keyword">new</span> Account();</span><br><span class="line">		</span><br><span class="line">		pay.setId(<span class="number">1</span>);</span><br><span class="line">		pay.setTranferMoney(tranferMoney);</span><br><span class="line">		</span><br><span class="line">		<span class="comment">//先扣款</span></span><br><span class="line">		mapper.subMoney(pay);</span><br><span class="line">		</span><br><span class="line">		<span class="keyword">int</span> a = <span class="number">1</span>/<span class="number">0</span>;</span><br><span class="line">		Account collect = <span class="keyword">new</span> Account();</span><br><span class="line">		collect.setId(<span class="number">2</span>);</span><br><span class="line">		collect.setTranferMoney(tranferMoney);</span><br><span class="line">		<span class="comment">//加款</span></span><br><span class="line">		mapper.addMoney(collect);</span><br><span class="line"></span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>


        <h2 id="主函数调用"   >
          <a href="#主函数调用" class="heading-link"><i class="fas fa-link"></i></a><a href="#主函数调用" class="headerlink" title="主函数调用"></a>主函数调用</h2>
      <figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@RunWith(SpringJUnit4ClassRunner.class)</span></span><br><span class="line"><span class="meta">@ContextConfiguration(&quot;classpath:applicationContext.xml&quot;)</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">MapperTest</span> </span>&#123;</span><br><span class="line">	</span><br><span class="line">	<span class="meta">@Resource(name=&quot;accountService&quot;)</span></span><br><span class="line">	<span class="keyword">private</span> AccountService as;</span><br><span class="line">	</span><br><span class="line">	<span class="meta">@Test</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">Test1</span><span class="params">()</span> </span>&#123;</span><br><span class="line">		as.updateTranferAccount();</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>


        <h2 id="Spring配置文件"   >
          <a href="#Spring配置文件" class="heading-link"><i class="fas fa-link"></i></a><a href="#Spring配置文件" class="headerlink" title="Spring配置文件"></a>Spring配置文件</h2>
      
        <h3 id="applicationContext-xml-1"   >
          <a href="#applicationContext-xml-1" class="heading-link"><i class="fas fa-link"></i></a><a href="#applicationContext-xml-1" class="headerlink" title="applicationContext.xml"></a>applicationContext.xml</h3>
      <figure class="highlight xml"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">beans</span> <span class="attr">xmlns</span>=<span class="string">&quot;http://www.springframework.org/schema/beans&quot;</span></span></span><br><span class="line"><span class="tag">	<span class="attr">xmlns:xsi</span>=<span class="string">&quot;http://www.w3.org/2001/XMLSchema-instance&quot;</span></span></span><br><span class="line"><span class="tag">	<span class="attr">xmlns:context</span>=<span class="string">&quot;http://www.springframework.org/schema/context&quot;</span></span></span><br><span class="line"><span class="tag">	<span class="attr">xmlns:aop</span>=<span class="string">&quot;http://www.springframework.org/schema/aop&quot;</span></span></span><br><span class="line"><span class="tag">	<span class="attr">xmlns:tx</span>=<span class="string">&quot;http://www.springframework.org/schema/tx&quot;</span></span></span><br><span class="line"><span class="tag">	<span class="attr">xsi:schemaLocation</span>=<span class="string">&quot;http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd</span></span></span><br><span class="line"><span class="string"><span class="tag">		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd</span></span></span><br><span class="line"><span class="string"><span class="tag">		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd</span></span></span><br><span class="line"><span class="string"><span class="tag">		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd&quot;</span>&gt;</span></span><br><span class="line">		</span><br><span class="line">	<span class="comment">&lt;!-- 读取配置文件 --&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">context:property-placeholder</span> <span class="attr">location</span>=<span class="string">&quot;db.properties&quot;</span>/&gt;</span></span><br><span class="line">	</span><br><span class="line">	<span class="comment">&lt;!-- 配置 dataSource --&gt;</span></span><br><span class="line">	<span class="comment">&lt;!-- db.properties文件中分别有--&gt;</span></span><br><span class="line">	<span class="comment">&lt;!-- jdbc.driverClass=com.mysql.jdbc.Driver --&gt;</span></span><br><span class="line">	<span class="comment">&lt;!-- jdbc.jdbcUrl=jdbc:mysql://localhost:3306/ssm_spring --&gt;</span></span><br><span class="line">	<span class="comment">&lt;!-- jdbc.user=root --&gt;</span></span><br><span class="line">	<span class="comment">&lt;!-- jdbc.password=123 --&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">bean</span> <span class="attr">name</span>=<span class="string">&quot;dataSource&quot;</span> <span class="attr">class</span>=<span class="string">&quot;com.mchange.v2.c3p0.ComboPooledDataSource&quot;</span>&gt;</span></span><br><span class="line">		<span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">&quot;driverClass&quot;</span> <span class="attr">value</span>=<span class="string">&quot;$&#123;jdbc.driverClass&#125;&quot;</span>/&gt;</span></span><br><span class="line">		<span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">&quot;jdbcUrl&quot;</span> <span class="attr">value</span>=<span class="string">&quot;$&#123;jdbc.jdbcUrl&#125;&quot;</span>/&gt;</span></span><br><span class="line">		<span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">&quot;user&quot;</span> <span class="attr">value</span>=<span class="string">&quot;$&#123;jdbc.user&#125;&quot;</span>/&gt;</span></span><br><span class="line">		<span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">&quot;password&quot;</span> <span class="attr">value</span>=<span class="string">&quot;$&#123;jdbc.password&#125;&quot;</span>/&gt;</span></span><br><span class="line">	<span class="tag">&lt;/<span class="name">bean</span>&gt;</span></span><br><span class="line">	</span><br><span class="line">	<span class="comment">&lt;!-- mybatis --&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">bean</span> <span class="attr">name</span>=<span class="string">&quot;sqlSessionFactory&quot;</span> <span class="attr">class</span>=<span class="string">&quot;org.mybatis.spring.SqlSessionFactoryBean&quot;</span>&gt;</span></span><br><span class="line">		<span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">&quot;dataSource&quot;</span> <span class="attr">ref</span>=<span class="string">&quot;dataSource&quot;</span>/&gt;</span></span><br><span class="line">		<span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">&quot;configLocation&quot;</span> <span class="attr">value</span>=<span class="string">&quot;classpath:sqlMapConfig.xml&quot;</span>/&gt;</span></span><br><span class="line">	<span class="tag">&lt;/<span class="name">bean</span>&gt;</span></span><br><span class="line">	</span><br><span class="line">	<span class="comment">&lt;!-- mapper工厂 --&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">bean</span> <span class="attr">class</span>=<span class="string">&quot;org.mybatis.spring.mapper.MapperScannerConfigurer&quot;</span>&gt;</span></span><br><span class="line">		<span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">&quot;basePackage&quot;</span> <span class="attr">value</span>=<span class="string">&quot;com.sikiedu.mapper&quot;</span>/&gt;</span></span><br><span class="line">	<span class="tag">&lt;/<span class="name">bean</span>&gt;</span></span><br><span class="line">	</span><br><span class="line">	<span class="comment">&lt;!-- service --&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">bean</span> <span class="attr">name</span>=<span class="string">&quot;accountService&quot;</span> <span class="attr">class</span>=<span class="string">&quot;com.sikiedu.service.AccountServiceImpl&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">bean</span>&gt;</span></span><br><span class="line">	</span><br><span class="line">	<span class="comment">&lt;!-- 需要事务核心管理器 --&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">bean</span> <span class="attr">name</span>=<span class="string">&quot;transactionManager&quot;</span> <span class="attr">class</span>=<span class="string">&quot;org.springframework.jdbc.datasource.DataSourceTransactionManager&quot;</span>&gt;</span></span><br><span class="line">		<span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">&quot;dataSource&quot;</span> <span class="attr">ref</span>=<span class="string">&quot;dataSource&quot;</span>/&gt;</span></span><br><span class="line">	<span class="tag">&lt;/<span class="name">bean</span>&gt;</span></span><br><span class="line">	</span><br><span class="line">	<span class="comment">&lt;!-- 配置事务通知 --&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">tx:advice</span> <span class="attr">id</span>=<span class="string">&quot;txAdvice&quot;</span> <span class="attr">transaction-manager</span>=<span class="string">&quot;transactionManager&quot;</span>&gt;</span></span><br><span class="line">		<span class="tag">&lt;<span class="name">tx:attributes</span>&gt;</span></span><br><span class="line">			<span class="tag">&lt;<span class="name">tx:method</span> <span class="attr">name</span>=<span class="string">&quot;update*&quot;</span> <span class="attr">isolation</span>=<span class="string">&quot;DEFAULT&quot;</span> <span class="attr">propagation</span>=<span class="string">&quot;REQUIRED&quot;</span> <span class="attr">read-only</span>=<span class="string">&quot;false&quot;</span>/&gt;</span></span><br><span class="line">		<span class="tag">&lt;/<span class="name">tx:attributes</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;/<span class="name">tx:advice</span>&gt;</span></span><br><span class="line">	</span><br><span class="line">	<span class="comment">&lt;!-- 配置aop --&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">aop:config</span>&gt;</span></span><br><span class="line">		<span class="tag">&lt;<span class="name">aop:pointcut</span> <span class="attr">expression</span>=<span class="string">&quot;execution(* com.sikiedu.service.*ServiceImpl.*(..))&quot;</span> <span class="attr">id</span>=<span class="string">&quot;txPc&quot;</span>/&gt;</span></span><br><span class="line">		<span class="tag">&lt;<span class="name">aop:advisor</span> <span class="attr">advice-ref</span>=<span class="string">&quot;txAdvice&quot;</span> <span class="attr">pointcut-ref</span>=<span class="string">&quot;txPc&quot;</span>/&gt;</span></span><br><span class="line">	<span class="tag">&lt;/<span class="name">aop:config</span>&gt;</span></span><br><span class="line">	</span><br><span class="line"><span class="tag">&lt;/<span class="name">beans</span>&gt;</span>mapper工厂 --&gt;</span><br><span class="line">	<span class="tag">&lt;<span class="name">bean</span> <span class="attr">class</span>=<span class="string">&quot;org.mybatis.spring.mapper.MapperScannerConfigurer&quot;</span>&gt;</span></span><br><span class="line">		<span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">&quot;basePackage&quot;</span> <span class="attr">value</span>=<span class="string">&quot;com.sikiedu.mapper&quot;</span>/&gt;</span></span><br><span class="line">	<span class="tag">&lt;/<span class="name">bean</span>&gt;</span></span><br><span class="line">	</span><br><span class="line">	<span class="comment">&lt;!-- service --&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">bean</span> <span class="attr">name</span>=<span class="string">&quot;accountService&quot;</span> <span class="attr">class</span>=<span class="string">&quot;com.sikiedu.service.AccountServiceImpl&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">bean</span>&gt;</span></span><br><span class="line">	</span><br><span class="line">	<span class="comment">&lt;!-- 需要事务核心管理器 --&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">bean</span> <span class="attr">name</span>=<span class="string">&quot;transactionManager&quot;</span> <span class="attr">class</span>=<span class="string">&quot;org.springframework.jdbc.datasource.DataSourceTransactionManager&quot;</span>&gt;</span></span><br><span class="line">		<span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">&quot;dataSource&quot;</span> <span class="attr">ref</span>=<span class="string">&quot;dataSource&quot;</span>/&gt;</span></span><br><span class="line">	<span class="tag">&lt;/<span class="name">bean</span>&gt;</span></span><br><span class="line">	</span><br><span class="line">	<span class="comment">&lt;!-- 配置事务通知 --&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">tx:advice</span> <span class="attr">id</span>=<span class="string">&quot;txAdvice&quot;</span> <span class="attr">transaction-manager</span>=<span class="string">&quot;transactionManager&quot;</span>&gt;</span></span><br><span class="line">		<span class="tag">&lt;<span class="name">tx:attributes</span>&gt;</span></span><br><span class="line">			<span class="tag">&lt;<span class="name">tx:method</span> <span class="attr">name</span>=<span class="string">&quot;update*&quot;</span> <span class="attr">isolation</span>=<span class="string">&quot;DEFAULT&quot;</span> <span class="attr">propagation</span>=<span class="string">&quot;REQUIRED&quot;</span> <span class="attr">read-only</span>=<span class="string">&quot;false&quot;</span>/&gt;</span></span><br><span class="line">		<span class="tag">&lt;/<span class="name">tx:attributes</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;/<span class="name">tx:advice</span>&gt;</span></span><br><span class="line">	</span><br><span class="line">	<span class="comment">&lt;!-- 配置aop --&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">aop:config</span>&gt;</span></span><br><span class="line">		<span class="tag">&lt;<span class="name">aop:pointcut</span> <span class="attr">expression</span>=<span class="string">&quot;execution(* com.sikiedu.service.*ServiceImpl.*(..))&quot;</span> <span class="attr">id</span>=<span class="string">&quot;txPc&quot;</span>/&gt;</span></span><br><span class="line">		<span class="tag">&lt;<span class="name">aop:advisor</span> <span class="attr">advice-ref</span>=<span class="string">&quot;txAdvice&quot;</span> <span class="attr">pointcut-ref</span>=<span class="string">&quot;txPc&quot;</span>/&gt;</span></span><br><span class="line">	<span class="tag">&lt;/<span class="name">aop:config</span>&gt;</span></span><br><span class="line">	</span><br><span class="line"><span class="tag">&lt;/<span class="name">beans</span>&gt;</span></span><br><span class="line"></span><br></pre></td></tr></table></div></figure>


        <h2 id="Mybatis配置文件"   >
          <a href="#Mybatis配置文件" class="heading-link"><i class="fas fa-link"></i></a><a href="#Mybatis配置文件" class="headerlink" title="Mybatis配置文件"></a>Mybatis配置文件</h2>
      
        <h3 id="sqlMapConfig-xml"   >
          <a href="#sqlMapConfig-xml" class="heading-link"><i class="fas fa-link"></i></a><a href="#sqlMapConfig-xml" class="headerlink" title="sqlMapConfig.xml"></a>sqlMapConfig.xml</h3>
      <figure class="highlight xml"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; ?&gt;</span></span><br><span class="line"><span class="meta">&lt;!DOCTYPE <span class="meta-keyword">configuration</span></span></span><br><span class="line"><span class="meta">  <span class="meta-keyword">PUBLIC</span> <span class="meta-string">&quot;-//mybatis.org//DTD Config 3.0//EN&quot;</span></span></span><br><span class="line"><span class="meta">  <span class="meta-string">&quot;http://mybatis.org/dtd/mybatis-3-config.dtd&quot;</span>&gt;</span></span><br><span class="line">  </span><br><span class="line">  <span class="tag">&lt;<span class="name">configuration</span>&gt;</span></span><br><span class="line">  	<span class="tag">&lt;<span class="name">typeAliases</span>&gt;</span></span><br><span class="line">  		<span class="tag">&lt;<span class="name">package</span> <span class="attr">name</span>=<span class="string">&quot;com.sikiedu.bean&quot;</span>/&gt;</span></span><br><span class="line">  	<span class="tag">&lt;/<span class="name">typeAliases</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;/<span class="name">configuration</span>&gt;</span></span><br></pre></td></tr></table></div></figure>


        <h1 id="Spring实例三-Spring-AOP"   >
          <a href="#Spring实例三-Spring-AOP" class="heading-link"><i class="fas fa-link"></i></a><a href="#Spring实例三-Spring-AOP" class="headerlink" title="Spring实例三 Spring+AOP"></a>Spring实例三 Spring+AOP</h1>
      
        <h2 id="自定义通知类-MyAdvice-java"   >
          <a href="#自定义通知类-MyAdvice-java" class="heading-link"><i class="fas fa-link"></i></a><a href="#自定义通知类-MyAdvice-java" class="headerlink" title="自定义通知类 MyAdvice.java"></a>自定义通知类 MyAdvice.java</h2>
      <figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">MyAdvice</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">	<span class="comment">//before 前置通知 在目标方法前调用</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">before</span><span class="params">()</span> </span>&#123;</span><br><span class="line">		System.out.println(<span class="string">&quot;before&quot;</span>);</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	<span class="comment">//after 最终通知（后置通知）在目标方法后调用，无论是否出现异常都会执行 finally</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">after</span><span class="params">()</span> </span>&#123;</span><br><span class="line">		System.out.println(<span class="string">&quot;after&quot;</span>);</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	<span class="comment">//afterReturning 成功通知（后置通知）在目标方法执行后，并且执行成功，如果方法出现异常则不调用</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">afterReturning</span><span class="params">()</span> </span>&#123;</span><br><span class="line">		System.out.println(<span class="string">&quot;afterReturning&quot;</span>);</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	<span class="comment">//afterThrowing 异常通知（后置通知）在目标方法执行出现异常的时候才会调用</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">afterThrowing</span><span class="params">()</span> </span>&#123;</span><br><span class="line">		System.out.println(<span class="string">&quot;afterThrowing&quot;</span>);</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	<span class="comment">//around 环绕通知 需要我们手动调用目标方法，并且可以设置通知</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> Object <span class="title">around</span><span class="params">(ProceedingJoinPoint pjp)</span> <span class="keyword">throws</span> Throwable </span>&#123;</span><br><span class="line">		System.out.println(<span class="string">&quot;around before&quot;</span>);</span><br><span class="line">		Object proceed = pjp.proceed();</span><br><span class="line">		System.out.println(<span class="string">&quot;around after&quot;</span>);</span><br><span class="line">		<span class="keyword">return</span> proceed;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>


        <h2 id="Service以及实现类impl"   >
          <a href="#Service以及实现类impl" class="heading-link"><i class="fas fa-link"></i></a><a href="#Service以及实现类impl" class="headerlink" title="Service以及实现类impl"></a>Service以及实现类impl</h2>
      
        <h3 id="UserService-java"   >
          <a href="#UserService-java" class="heading-link"><i class="fas fa-link"></i></a><a href="#UserService-java" class="headerlink" title="UserService.java"></a>UserService.java</h3>
      <figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">interface</span> <span class="title">UserService</span> </span>&#123;</span><br><span class="line">	<span class="comment">//增</span></span><br><span class="line">	<span class="function"><span class="keyword">void</span> <span class="title">save</span><span class="params">()</span></span>;</span><br><span class="line">	<span class="comment">//删</span></span><br><span class="line">	<span class="function"><span class="keyword">void</span> <span class="title">delete</span><span class="params">()</span></span>;</span><br><span class="line">	<span class="comment">//改</span></span><br><span class="line">	<span class="function"><span class="keyword">void</span> <span class="title">update</span><span class="params">()</span></span>;</span><br><span class="line">	<span class="comment">//查</span></span><br><span class="line">	<span class="function"><span class="keyword">void</span> <span class="title">find</span><span class="params">()</span></span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>


        <h3 id="UserServiceImpl-java"   >
          <a href="#UserServiceImpl-java" class="heading-link"><i class="fas fa-link"></i></a><a href="#UserServiceImpl-java" class="headerlink" title="UserServiceImpl.java"></a>UserServiceImpl.java</h3>
      <figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">UserServiceImpl</span> <span class="keyword">implements</span> <span class="title">UserService</span> </span>&#123;</span><br><span class="line">	<span class="meta">@Override</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">save</span><span class="params">()</span> </span>&#123;</span><br><span class="line">		System.out.println(<span class="string">&quot;save&quot;</span>);</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="meta">@Override</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">delete</span><span class="params">()</span> </span>&#123;</span><br><span class="line">		System.out.println(<span class="string">&quot;delete&quot;</span>);</span><br><span class="line">		<span class="comment">//int a = 1/0;</span></span><br><span class="line">	&#125;</span><br><span class="line">	<span class="meta">@Override</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">update</span><span class="params">()</span> </span>&#123;</span><br><span class="line">		System.out.println(<span class="string">&quot;update&quot;</span>);</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="meta">@Override</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">find</span><span class="params">()</span> </span>&#123;</span><br><span class="line">		System.out.println(<span class="string">&quot;find&quot;</span>);</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>


        <h2 id="Spring配置文件-applicationContext-xml"   >
          <a href="#Spring配置文件-applicationContext-xml" class="heading-link"><i class="fas fa-link"></i></a><a href="#Spring配置文件-applicationContext-xml" class="headerlink" title="Spring配置文件 applicationContext.xml"></a>Spring配置文件 applicationContext.xml</h2>
      <figure class="highlight xml"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">beans</span> <span class="attr">xmlns</span>=<span class="string">&quot;http://www.springframework.org/schema/beans&quot;</span></span></span><br><span class="line"><span class="tag">	<span class="attr">xmlns:xsi</span>=<span class="string">&quot;http://www.w3.org/2001/XMLSchema-instance&quot;</span></span></span><br><span class="line"><span class="tag">	<span class="attr">xmlns:aop</span>=<span class="string">&quot;http://www.springframework.org/schema/aop&quot;</span></span></span><br><span class="line"><span class="tag">	<span class="attr">xsi:schemaLocation</span>=<span class="string">&quot;http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd</span></span></span><br><span class="line"><span class="string"><span class="tag">		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd&quot;</span>&gt;</span></span><br><span class="line">		</span><br><span class="line">		<span class="comment">&lt;!-- 目标对象 --&gt;</span></span><br><span class="line">		<span class="tag">&lt;<span class="name">bean</span> <span class="attr">name</span>=<span class="string">&quot;userService&quot;</span> <span class="attr">class</span>=<span class="string">&quot;com.sikiedu.service.UserServiceImpl&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">bean</span>&gt;</span></span><br><span class="line">		</span><br><span class="line">		<span class="comment">&lt;!-- 通知对象 --&gt;</span></span><br><span class="line">		<span class="tag">&lt;<span class="name">bean</span> <span class="attr">name</span>=<span class="string">&quot;myAdvice&quot;</span> <span class="attr">class</span>=<span class="string">&quot;com.sikiedu.aop.MyAdvice&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">bean</span>&gt;</span></span><br><span class="line">		</span><br><span class="line">		<span class="tag">&lt;<span class="name">aop:config</span>&gt;</span></span><br><span class="line">			<span class="comment">&lt;!-- 切入点  expression 切入点表达式 可以配置要增强的方法</span></span><br><span class="line"><span class="comment">								public void com.sikiedu.service.UserServiceImpl.save()</span></span><br><span class="line"><span class="comment">								* com.sikiedu.service.*ServiceImpl.*(..)</span></span><br><span class="line"><span class="comment">							   id 就是唯一标识</span></span><br><span class="line"><span class="comment">			--&gt;</span></span><br><span class="line">			<span class="tag">&lt;<span class="name">aop:pointcut</span> <span class="attr">expression</span>=<span class="string">&quot;execution(* com.sikiedu.service.*ServiceImplabc.*(..))&quot;</span> <span class="attr">id</span>=<span class="string">&quot;servicePc&quot;</span>/&gt;</span></span><br><span class="line">			</span><br><span class="line">			<span class="comment">&lt;!-- 切面 通知+切入点 --&gt;</span></span><br><span class="line">			<span class="tag">&lt;<span class="name">aop:aspect</span> <span class="attr">ref</span>=<span class="string">&quot;myAdvice&quot;</span>&gt;</span></span><br><span class="line">				<span class="comment">&lt;!-- 通知类型 --&gt;</span></span><br><span class="line">				<span class="tag">&lt;<span class="name">aop:before</span> <span class="attr">method</span>=<span class="string">&quot;before&quot;</span> <span class="attr">pointcut-ref</span>=<span class="string">&quot;servicePc&quot;</span>/&gt;</span></span><br><span class="line">				<span class="comment">&lt;!-- 最终通知 后置通知 --&gt;</span></span><br><span class="line">				<span class="tag">&lt;<span class="name">aop:after</span> <span class="attr">method</span>=<span class="string">&quot;after&quot;</span> <span class="attr">pointcut-ref</span>=<span class="string">&quot;servicePc&quot;</span>/&gt;</span></span><br><span class="line">				<span class="comment">&lt;!-- 成功通知 后置通知 --&gt;</span></span><br><span class="line">				<span class="tag">&lt;<span class="name">aop:after-returning</span> <span class="attr">method</span>=<span class="string">&quot;afterReturning&quot;</span> <span class="attr">pointcut-ref</span>=<span class="string">&quot;servicePc&quot;</span>/&gt;</span></span><br><span class="line">				<span class="comment">&lt;!-- 异常通知 后置通知 --&gt;</span></span><br><span class="line">				<span class="tag">&lt;<span class="name">aop:after-throwing</span> <span class="attr">method</span>=<span class="string">&quot;afterThrowing&quot;</span> <span class="attr">pointcut-ref</span>=<span class="string">&quot;servicePc&quot;</span>/&gt;</span></span><br><span class="line">				<span class="comment">&lt;!-- 环绕通知 --&gt;</span></span><br><span class="line">				<span class="tag">&lt;<span class="name">aop:around</span> <span class="attr">method</span>=<span class="string">&quot;around&quot;</span> <span class="attr">pointcut-ref</span>=<span class="string">&quot;servicePc&quot;</span>/&gt;</span></span><br><span class="line">			<span class="tag">&lt;/<span class="name">aop:aspect</span>&gt;</span></span><br><span class="line">		<span class="tag">&lt;/<span class="name">aop:config</span>&gt;</span></span><br><span class="line">		</span><br><span class="line"><span class="tag">&lt;/<span class="name">beans</span>&gt;</span></span><br></pre></td></tr></table></div></figure>


        <h2 id="主函数测试方法"   >
          <a href="#主函数测试方法" class="heading-link"><i class="fas fa-link"></i></a><a href="#主函数测试方法" class="headerlink" title="主函数测试方法"></a>主函数测试方法</h2>
      
        <h3 id="AopTest-java"   >
          <a href="#AopTest-java" class="heading-link"><i class="fas fa-link"></i></a><a href="#AopTest-java" class="headerlink" title="AopTest.java"></a>AopTest.java</h3>
      <figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@RunWith(SpringJUnit4ClassRunner.class)</span></span><br><span class="line"><span class="meta">@ContextConfiguration(&quot;classpath:applicationContext.xml&quot;)</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">AopTest</span> </span>&#123;</span><br><span class="line">	</span><br><span class="line">	<span class="meta">@Resource(name=&quot;userService&quot;)</span></span><br><span class="line">	UserService us;</span><br><span class="line">	</span><br><span class="line">	<span class="meta">@Test</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">Test1</span><span class="params">()</span> </span>&#123;</span><br><span class="line">		UserServiceProxy usProxy = <span class="keyword">new</span> UserServiceProxy();</span><br><span class="line">		UserService us = <span class="keyword">new</span> UserServiceImpl();</span><br><span class="line">		UserService us_PowerUp = usProxy.getUserServiceProxy(us);</span><br><span class="line">		us_PowerUp.find();</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	<span class="meta">@Test</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">Test2</span><span class="params">()</span> </span>&#123;</span><br><span class="line">		us.delete();</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>


        <h3 id="UserServiceProxy-java"   >
          <a href="#UserServiceProxy-java" class="heading-link"><i class="fas fa-link"></i></a><a href="#UserServiceProxy-java" class="headerlink" title="UserServiceProxy.java"></a>UserServiceProxy.java</h3>
      <figure class="highlight java"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"><span class="keyword">package</span> com.sikiedu.test;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.lang.reflect.InvocationHandler;</span><br><span class="line"><span class="keyword">import</span> java.lang.reflect.Method;</span><br><span class="line"><span class="keyword">import</span> java.lang.reflect.Proxy;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> com.sikiedu.service.UserService;</span><br><span class="line"><span class="keyword">import</span> com.sikiedu.service.UserServiceImpl;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * UserService代理类</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@author</span> Joey</span></span><br><span class="line"><span class="comment"> *</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">UserServiceProxy</span> </span>&#123;</span><br><span class="line">	<span class="comment">//UserServiceProxy</span></span><br><span class="line">	<span class="comment">//UserService</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> UserService <span class="title">getUserServiceProxy</span><span class="params">(UserService us)</span> </span>&#123;</span><br><span class="line">		</span><br><span class="line">	<span class="keyword">return</span> (UserService) Proxy.newProxyInstance(UserServiceProxy.class.getClassLoader(), </span><br><span class="line">		UserServiceImpl.class.getInterfaces(), </span><br><span class="line">		<span class="keyword">new</span> InvocationHandler() &#123;</span><br><span class="line">		<span class="meta">@Override</span></span><br><span class="line">		<span class="function"><span class="keyword">public</span> Object <span class="title">invoke</span><span class="params">(Object proxy, </span></span></span><br><span class="line"><span class="params"><span class="function">				Method method, </span></span></span><br><span class="line"><span class="params"><span class="function">				Object[] args)</span> <span class="keyword">throws</span> Throwable </span>&#123;</span><br><span class="line">			<span class="comment">//增强代码</span></span><br><span class="line">			System.out.println(<span class="string">&quot;开启事务&quot;</span>);</span><br><span class="line">			<span class="comment">//调用原始方法</span></span><br><span class="line">			Object invoke = method.invoke(us, args);</span><br><span class="line">			System.out.println(<span class="string">&quot;提交/回滚&quot;</span>);</span><br><span class="line">			<span class="keyword">return</span> invoke;</span><br><span class="line">		&#125;</span><br><span class="line">		&#125;);</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></div></figure></div></div></article><article class="postlist-item post"><header class="post-header"><h1 class="post-title"><a class="post-title__link" href="/blog/2021/03/15/JavaScript/">JavaScript学习笔记</a></h1><div class="post-meta"><span class="post-meta-item post-meta-item--createtime"><span class="post-meta-item__icon"><i class="far fa-calendar-plus"></i></span><span class="post-meta-item__info">发表于</span><span class="post-meta-item__value">2021-03-15</span></span><span class="post-meta-item post-meta-item--updatetime"><span class="post-meta-item__icon"><i class="far fa-calendar-check"></i></span><span class="post-meta-item__info">更新于</span><span class="post-meta-item__value">2021-04-23</span></span><span class="post-meta-item post-meta-item--wordcount"><span class="post-meta-item__icon"><i class="far fa-file-word"></i></span><span class="post-meta-item__info">字数统计</span><span class="post-meta-item__value">3.6k</span></span><span class="post-meta-item post-meta-item--readtime"><span class="post-meta-item__icon"><i class="far fa-clock"></i></span><span class="post-meta-item__info">阅读时长</span><span class="post-meta-item__value">24分</span></span></div></header><div class="post-body"><div class="post-excerpt"><!-- 页面必备样式 -->
<style>
.back-top{position: fixed;top: 500px;height: 0px;float: right;right: 50px;}
.back-top>a{font-weight:bold;width: 50px;height: 50px;text-align: center;text-decoration: none;font-size: 5px;line-height: 50px;border-radius: 50px;background-color:rgba(255,255,255,0.5);}
</style>
<div class="back-top">
    <a href="#top"> >返回顶部< </a>
</div>
<!-- toc -->
<!-- 必备样式结束 -->


        <h1 id="介绍"   >
          <a href="#介绍" class="heading-link"><i class="fas fa-link"></i></a><a href="#介绍" class="headerlink" title="介绍"></a>介绍</h1>
      
        <h2 id="前言"   >
          <a href="#前言" class="heading-link"><i class="fas fa-link"></i></a><a href="#前言" class="headerlink" title="前言"></a>前言</h2>
      <p>JavaScript一种直译式脚本语言，是一种动态类型、弱类型、基于原型的语言，内置支持类型。它的解释器被称为JavaScript引擎，为浏览器的一部分，广泛用于客户端的脚本语言，最早是在HTML（标准通用标记语言下的一个应用）网页上使用，用来给HTML网页增加动态功能。—— 百度百科</p>
<p>学习了HTML和CSS3后，我们可以对网页简单的静态“整容”，但是很难通过后期的需求来对网页动态的增删改查；因此，学习动态的脚本语言JavaScript（JS）是非常有必要的。</p>

        <h1 id="正文"   >
          <a href="#正文" class="heading-link"><i class="fas fa-link"></i></a><a href="#正文" class="headerlink" title="正文"></a>正文</h1>
      
        <h2 id="Java与JavaScript的关系"   >
          <a href="#Java与JavaScript的关系" class="heading-link"><i class="fas fa-link"></i></a><a href="#Java与JavaScript的关系" class="headerlink" title="Java与JavaScript的关系"></a>Java与JavaScript的关系</h2>
      <p>
第一次学JS的时候，看到单词是JavaScript，总觉得是Java加上脚本script，

<p>因此心中一直有误解；<mark>其实，虽然JS是脚本语言不错，但是编程上是与Java没有任何关系的</mark>。</p>
</p>


<div class="table-container"><table class="table table-bordered table-hover table-striped">
<tr>
    <td>JavaScript的特点</td>
    <td>Java的特点</td>
</tr>
<tr>
    <td>解释性语言，无需事先编译</td>
    <td>编译性语言，会检查语法，不正确无法通过运行</td>
</tr>
<tr>
    <td>基于对象，拥有封装的特性，但是没有继承与多态</td>
    <td>面向对象，拥有封装、继承、多态等特性</td>
</tr>
<tr>
    <td>事件源触发事件驱动</td>
    <td>主函数入口驱动</td>
</tr>
<tr>
    <td>依赖于浏览器，不同的浏览器可能导致效果不同</td>
    <td>跨平台使用，依赖于JVM（Java Virtual Machine)</td>
</tr>
<tr>
    <td>安全，依赖于浏览器但不访问硬盘</td>
    <td>安全与否取决于通过字节码检验</td>
</tr>
<tr>
    <td>弱变量，解释器自动通过变量值来判断类型</td>
    <td>强变量，变量声明时需要通过变量检查</td>
</tr>
<tr>
    <td>厂家是Netscape公司</td>
    <td>厂家是sum公司</td>
</tr>
</table></div>


        <h2 id="JavaScript-基础"   >
          <a href="#JavaScript-基础" class="heading-link"><i class="fas fa-link"></i></a><a href="#JavaScript-基础" class="headerlink" title="JavaScript 基础"></a>JavaScript 基础</h2>
      
        <h3 id="JavaScript-引用语法"   >
          <a href="#JavaScript-引用语法" class="heading-link"><i class="fas fa-link"></i></a><a href="#JavaScript-引用语法" class="headerlink" title="JavaScript 引用语法"></a>JavaScript 引用语法</h3>
      <p>引用JavaScript的方法有三种，与CSS相似，可以在网页的头部引用，可以用script标签，也可以作为外部JS文件来引用。</p>
<p>值得注意的是： 使用script标签时，我们最好放在元素后方 ，否则无法获取该DOM对象（将使用实例说明）。</p>
<ol>
<li><p>使用 window.alert(“内容”); 弹出警告框</p>
</li>
<li><p>使用 document.write(“内容”); 写入网页文档中</p>
</li>
<li><p>改变DOM对象的 innerHTML 属性</p>
</li>
<li><p>使用控制台 console.log(“内容”) 写入内容</p>
</li>
</ol>

        <h3 id="JavaScript-语句与变量"   >
          <a href="#JavaScript-语句与变量" class="heading-link"><i class="fas fa-link"></i></a><a href="#JavaScript-语句与变量" class="headerlink" title="JavaScript 语句与变量"></a>JavaScript 语句与变量</h3>
      <p>在JS中，我们用分号;隔开语句，<mark>但这也是可选的，只是建议为了编程规范用分号隔开每条语句</mark>；</p>
<p>浏览器将依次按顺序执行每条语句。</p>
<p>在JS中，我们用花括号{}隔开代码块，在代码块中区别不同的语句。</p>
<p>JavaScript中的变量都是弱变量，即我们不需要像Java那样声明 int a = 1; String str = “hello”; 只需要全部统一使用 var v = 1; var str = “hello”; 进行声明与赋值，甚至连 var 都可以省去。</p>
<p>JavaScript对大小写敏感，因此变量名为a和A是属于不同的两个变量；变量不能以数字开头，也可以 美元字符$ 和下划线_ 开头。</p>
<figure class="highlight js"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> a = <span class="number">1</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">var</span> b = <span class="number">2</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">var</span> c = a + b;</span><br><span class="line"></span><br><span class="line">windows.alert(<span class="string">&quot;c的结果为:&quot;</span> + c);</span><br></pre></td></tr></table></div></figure>


        <h3 id="JavaScript-注释"   >
          <a href="#JavaScript-注释" class="heading-link"><i class="fas fa-link"></i></a><a href="#JavaScript-注释" class="headerlink" title="JavaScript 注释"></a>JavaScript 注释</h3>
      <p>在JS中，注释可分为单行与多行注释两种。</p>
<p>单行注释使用双斜线 // 开始注释，并在后紧跟注释内容。多行注释使用 /** 注释内容 ***/  ，中间可以跨度若干行。</p>

        <h3 id="JavaScript-数据类型"   >
          <a href="#JavaScript-数据类型" class="heading-link"><i class="fas fa-link"></i></a><a href="#JavaScript-数据类型" class="headerlink" title="JavaScript 数据类型"></a>JavaScript 数据类型</h3>
      <p>JavaScript中的基本类型有五种：</p>
<p>String字符串、Number数字、Boolean布尔、NULL空值 和 Undefined未定义的值；</p>
<p>而 引用数据类型 有:Object对象、 Array数组和Function函数。</p>

        <h4 id="JavaScript-字符串-String"   >
          <a href="#JavaScript-字符串-String" class="heading-link"><i class="fas fa-link"></i></a><a href="#JavaScript-字符串-String" class="headerlink" title="JavaScript 字符串 String"></a>JavaScript 字符串 String</h4>
      <p>存储字符的变量，我们称之为字符串变量，我们可以用单引号‘’或者双引号“”将任意文本包裹起来，</p>
<p>但要求如果文本中需要引号，不与外部引号相同即可。即内单外双，内双外单。例如：</p>
<figure class="highlight js"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> pc = <span class="string">&quot;ASUS&quot;</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">var</span> words = <span class="string">&quot;is the famous brand&quot;</span>;</span><br><span class="line"></span><br><span class="line">windows.alert(pc + words);</span><br></pre></td></tr></table></div></figure>

<p>我们可以使用 索引 来访问字符串中任何字符，使用数组的下标[x]，其中[x]从0开始（第一个字符）到n-1（最后一个字符）结束，n为字符串中字符的个数。</p>
<p>使用定位索引 indexOf() 来从头定位某一个指定的字符首次出现的位置，出现的位置亦是从0开始；</p>
<p>使用 lastIndexOf() 从末尾开始寻找第一次出现的位置。</p>
<p>使用 replace() 方法在字符串中替换字符，例如：</p>
<figure class="highlight js"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> str1 = <span class="string">&quot;Apple is pretty good!&quot;</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">var</span> str2 = str1.replace(<span class="string">&quot;Apple&quot;</span>, <span class="string">&quot;HuaWei&quot;</span>); </span><br><span class="line"></span><br><span class="line">windows.alert(str2);</span><br></pre></td></tr></table></div></figure>

<p>使用 toUpperCase()和toLowerCase() 分别将字符小写转换成大写和大写转换为小写；</p>
<p>使用 split(“x”) 方法将字符串转换为数组，其中引号中的内容为分隔成数组的要求。</p>
<p><strong>JavaScript中String字符串的常用方法及说明：</strong></p>
<figure class="highlight js"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">charAt(x) <span class="comment">//返回位于x处的字符（0~length()-1)</span></span><br><span class="line">	</span><br><span class="line">compareTo(str2) <span class="comment">//str1使用该方法，与str2按字典序进行比较，结果为0，小于0，大于0的数，分别代表字符相等，str1小于str2与str1大于str2</span></span><br><span class="line">	</span><br><span class="line">matches(reg) <span class="comment">//reg是给定的正则表达式，使用该方法匹配是否字符串能满足该表达式</span></span><br><span class="line"></span><br><span class="line">trim() <span class="comment">//返回字符串忽略前部空白与尾部空白后的结果</span></span><br></pre></td></tr></table></div></figure>


        <h4 id="JavaScript-数字-Number"   >
          <a href="#JavaScript-数字-Number" class="heading-link"><i class="fas fa-link"></i></a><a href="#JavaScript-数字-Number" class="headerlink" title="JavaScript 数字 Number"></a>JavaScript 数字 Number</h4>
      <p>我们可以用两种方法来表示数字，一是直接书写数字值，二是利用科学计数法来表示：</p>
<figure class="highlight js"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> x = <span class="number">1.222</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">var</span> y = <span class="number">2e3</span>;  <span class="comment">//代表2000 2*10^3 </span></span><br></pre></td></tr></table></div></figure>

<p><strong>八进制与十六进制</strong></p>
<p>默认情况下，JavaScript中的数字是使用十进制显示；</p>
<p>如果前缀是0，那么代表是八进制数，如果是0x则为十六进制数。</p>
<p><strong>无穷大 Infinity</strong></p>
<p>当数字运算的结果超出了JS的数字表示结果时，则会出现一个无穷大值，以infinity表示，负无穷大以-infinity表示：<br></p>
<figure class="highlight js"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> a = <span class="number">2</span> / <span class="number">0</span>; <span class="comment">// a为正无穷大</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">var</span> b = -<span class="number">2</span> / <span class="number">0</span>; <span class="comment">//b为负无穷大 </span></span><br></pre></td></tr></table></div></figure>
<p><strong>非数字 NaN(not a number)</strong></p>
<p><strong>NaN代表某值不是数字，同时我们可以使用isNaN(x)方法来判断x是不是NaN。</strong></p>

        <h4 id="JavaScript-布尔-Boolean"   >
          <a href="#JavaScript-布尔-Boolean" class="heading-link"><i class="fas fa-link"></i></a><a href="#JavaScript-布尔-Boolean" class="headerlink" title="JavaScript 布尔 Boolean"></a>JavaScript 布尔 Boolean</h4>
      <p>布尔只能有两个值，即true或false。</p>

        <h4 id="JavaScript-数组-Array"   >
          <a href="#JavaScript-数组-Array" class="heading-link"><i class="fas fa-link"></i></a><a href="#JavaScript-数组-Array" class="headerlink" title="JavaScript 数组 Array"></a>JavaScript 数组 Array</h4>
      <p>使用Array来存储一系列的值，值可以是不同的类型。</p>
<p>创建数组</p>
<ol>
<li><p>先new声明后赋值</p>
<figure class="highlight js"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> array_name = <span class="keyword">new</span> <span class="built_in">Array</span>();</span><br><span class="line"></span><br><span class="line">array_name[<span class="number">0</span>] = value1;</span><br><span class="line"></span><br><span class="line">array_name[<span class="number">1</span>] = value2;</span><br><span class="line"></span><br><span class="line">array_name[<span class="number">2</span>] = value1;	</span><br></pre></td></tr></table></div></figure></li>
<li><p>边声明边赋值</p>
<figure class="highlight js"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> array_name = <span class="keyword">new</span> <span class="built_in">Array</span>(value1, value2, value3);</span><br></pre></td></tr></table></div></figure></li>
<li><p>直接赋值</p>
</li>
</ol>
<figure class="highlight js"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> people = [value1, value2, value3];</span><br></pre></td></tr></table></div></figure>

<p><strong>常用数组方法及说明：</strong></p>
<figure class="highlight js"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">array1.concat(array2) <span class="comment">//合并数组</span></span><br><span class="line"></span><br><span class="line">join() <span class="comment">//将数组的元素组成字符串</span></span><br><span class="line"></span><br><span class="line">pop() <span class="comment">//删除数组的最后一个元素</span></span><br><span class="line"></span><br><span class="line">push() <span class="comment">//数组的末尾添加新的元素</span></span><br><span class="line"></span><br><span class="line">shift() <span class="comment">//删除数组的第一个元素</span></span><br><span class="line"></span><br><span class="line">unshift() <span class="comment">//数组的开头添加新的元素</span></span><br></pre></td></tr></table></div></figure>


        <h4 id="JavaScript-对象-Object"   >
          <a href="#JavaScript-对象-Object" class="heading-link"><i class="fas fa-link"></i></a><a href="#JavaScript-对象-Object" class="headerlink" title="JavaScript 对象 Object"></a>JavaScript 对象 Object</h4>
      <p>由于JavaScript是基于对象的，因此所有一切皆为对象。对象拥有自己的方法和属性。</p>
<p><strong>自定义对象</strong></p>
<figure class="highlight js"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> person = <span class="keyword">new</span> <span class="built_in">Object</span>();</span><br><span class="line"></span><br><span class="line">person.firstname = <span class="string">&quot;jack&quot;</span>;</span><br><span class="line"></span><br><span class="line">person.age = <span class="number">9</span>;</span><br></pre></td></tr></table></div></figure>

<p><strong>使用构造方法</strong></p>
<figure class="highlight js"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">person</span>(<span class="params">firstname, age</span>)</span>&#123; </span><br><span class="line">	<span class="built_in">this</span>.firstname = firstname;</span><br><span class="line">	</span><br><span class="line">	<span class="built_in">this</span>.age = age;</span><br><span class="line">&#125; </span><br><span class="line"></span><br><span class="line"><span class="keyword">var</span> brother = <span class="keyword">new</span> person(<span class="string">&quot;jack&quot;</span>, <span class="number">9</span>);</span><br></pre></td></tr></table></div></figure>


        <h3 id="JavaScript-函数"   >
          <a href="#JavaScript-函数" class="heading-link"><i class="fas fa-link"></i></a><a href="#JavaScript-函数" class="headerlink" title="JavaScript 函数"></a>JavaScript 函数</h3>
      <p>函数就是包裹在花括号中的代码块：</p>
<figure class="highlight js"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">funName</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>

<p><strong>Javascript 对大小写敏感</strong></p>
<p>可以传入参数，在调用函数时，写的个数与声明的参数个数顺序要一致：</p>
<figure class="highlight js"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">funName</span>(<span class="params">par1, par2</span>)</span>&#123;</span><br><span class="line">	</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>

<p><code>&lt;div onclick=&quot;funName(par1, par2)&quot;&gt;&lt;/div&gt;</code></p>
<p><strong>当你想从当前位置，使JavaScript停止往下执行代码时，可以使该函数在某个地方提前返回值</strong></p>
<p><strong>使用 return</strong></p>
<figure class="highlight js"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">funName</span>(<span class="params">para1, para2</span>)</span>&#123;</span><br><span class="line">	<span class="keyword">return</span> para1 + para2</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>


        <h3 id="局部JavaScript变量与全局JavaScript变量"   >
          <a href="#局部JavaScript变量与全局JavaScript变量" class="heading-link"><i class="fas fa-link"></i></a><a href="#局部JavaScript变量与全局JavaScript变量" class="headerlink" title="局部JavaScript变量与全局JavaScript变量"></a>局部JavaScript变量与全局JavaScript变量</h3>
      <p><strong>在函数内部定义的变量，我们一般称为局部变量，它的作用域只限于整个函数体</strong></p>
<p>相反，<strong>在函数外部定义的变量是全局的</strong></p>
<p><mark>全局的JS文件或者函数都可以访问这个变量</mark></p>
<figure class="highlight js"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> globalVar = <span class="number">12</span>;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">fun1</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">	<span class="keyword">var</span> partialVar1 = <span class="number">2</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">fun2</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">	<span class="keyword">var</span> partialVar2 = <span class="number">3</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>

<p><strong>可以看出，fun1与fun2都可以在任意处引用globalVar</strong></p>
<p><strong>值得注意的是，在VUE中的var变量一般会提升变量的作用域、而let变量不会（更推荐使用let变量）</strong></p>
<p><strong>JavaScript中的变量一定要先声明后赋值，不同于Python编程语言</strong></p>
<p>若某个变量没有x属性，强行使用或者调用该属性时，会报错<strong>No such property等错误</strong></p>

        <h1 id="常见的HTML事件"   >
          <a href="#常见的HTML事件" class="heading-link"><i class="fas fa-link"></i></a><a href="#常见的HTML事件" class="headerlink" title="常见的HTML事件"></a>常见的HTML事件</h1>
      <ol>
<li>点击事件：onclick</li>
<li>某个键盘按键被松开: onkeyup</li>
<li>页面完成加载 onload</li>
<li>元素失去焦点 onblur</li>
<li>表单元素中的内容发生改变时：onchange （多用于input、select、textarea)</li>
<li>元素获得焦点时 onfocus</li>
<li>表单重置时 onsubmit</li>
</ol>

        <h3 id="运算符"   >
          <a href="#运算符" class="heading-link"><i class="fas fa-link"></i></a><a href="#运算符" class="headerlink" title="运算符"></a>运算符</h3>
      <p><strong>+运算符用于将两个或者多个字符串变量，文本值连接起来</strong></p>
<figure class="highlight js"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">funName</span>(<span class="params">var1, var2</span>)</span>&#123;</span><br><span class="line"> <span class="keyword">return</span> var1 + var2</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>

<p>值得注意的是，</p>
<ol>
<li><p>字符串与数字相加时，结果是字符串</p>
</li>
<li><p>当数字与数字相加 结果是数字</p>
</li>
</ol>

        <h3 id="比较运算符"   >
          <a href="#比较运算符" class="heading-link"><i class="fas fa-link"></i></a><a href="#比较运算符" class="headerlink" title="比较运算符"></a>比较运算符</h3>
      <ol>
<li><p>== 值等于</p>
</li>
<li><p>=== 值以及类型都相等</p>
</li>
<li><p>！= 不等于</p>
</li>
</ol>
<p><strong>当某些变量赋值时，也可以用到条件运算符</strong></p>
<figure class="highlight js"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">varName = (age&gt;<span class="number">18</span>)? <span class="string">&quot;没有成年&quot;</span> : <span class="string">&quot;已经成年&quot;</span>;</span><br><span class="line"></span><br></pre></td></tr></table></div></figure>



        <h3 id="条件语句"   >
          <a href="#条件语句" class="heading-link"><i class="fas fa-link"></i></a><a href="#条件语句" class="headerlink" title="条件语句"></a>条件语句</h3>
      <ol>
<li><p>if 条件 真语句：假语句</p>
</li>
<li><p>if-else</p>
</li>
<li><p>if-else if- else  </p>
</li>
</ol>
<ol start="4">
<li>switch-case<figure class="highlight js"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">switch</span>(n)&#123;</span><br><span class="line">     <span class="keyword">case</span> <span class="number">1</span>:</span><br><span class="line">        <span class="comment">//代码块1</span></span><br><span class="line">     <span class="keyword">case</span> <span class="number">2</span>:</span><br><span class="line">        <span class="comment">//代码块2</span></span><br><span class="line">     <span class="keyword">default</span>：</span><br><span class="line">      <span class="comment">// 默认case都不执行时 执行deault</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure></li>
</ol>

        <h3 id="循环"   >
          <a href="#循环" class="heading-link"><i class="fas fa-link"></i></a><a href="#循环" class="headerlink" title="循环"></a>循环</h3>
      
        <h4 id="for循环"   >
          <a href="#for循环" class="heading-link"><i class="fas fa-link"></i></a><a href="#for循环" class="headerlink" title="for循环"></a>for循环</h4>
      <figure class="highlight js"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">for</span>(<span class="keyword">var</span> i=<span class="number">0</span>; i&lt;<span class="number">5</span>; i++)&#123;</span><br><span class="line">   <span class="built_in">document</span>.write(i); <span class="comment">//输出0 1 2 3 4</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>
<figure class="highlight js"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> person = &#123;<span class="attr">fname</span>:<span class="string">&quot;Bill&quot;</span>,lname=<span class="string">&quot;Gates&quot;</span>, age=<span class="number">56</span>&#125;; </span><br><span class="line"></span><br><span class="line"><span class="keyword">for</span>(<span class="keyword">var</span> key <span class="keyword">in</span> person)&#123;</span><br><span class="line">   txt += person[key] key是属性 person[key]是对应的属性值</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">#### <span class="keyword">while</span>循环</span><br><span class="line"></span><br><span class="line"><span class="string">``</span><span class="string">`js</span></span><br><span class="line"><span class="string">  </span></span><br><span class="line"><span class="string">var i = 0;</span></span><br><span class="line"><span class="string">var end = 10;</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">while(i &lt; end)&#123;</span></span><br><span class="line"><span class="string">   document.write(&#x27;i满足一直比0小！&#x27;);</span></span><br><span class="line"><span class="string">   i++;</span></span><br><span class="line"><span class="string">&#125;</span></span><br></pre></td></tr></table></div></figure>


        <h4 id="break与continue语句"   >
          <a href="#break与continue语句" class="heading-link"><i class="fas fa-link"></i></a><a href="#break与continue语句" class="headerlink" title="break与continue语句"></a>break与continue语句</h4>
      <p><strong>break语句指的是，后面的都不判断了，跳出整个循环体（不管是for还是while）</strong></p>
<p>而continue是指，跳出当前循环，进入下一个条件值继续执行。</p>
<p>比如：</p>
<figure class="highlight js"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> i;</span><br><span class="line"><span class="keyword">for</span>(i=<span class="number">0</span>; i&lt;<span class="number">20</span>; i++)&#123;</span><br><span class="line">  <span class="keyword">if</span>(i % <span class="number">3</span> == <span class="number">0</span>)&#123;</span><br><span class="line">      <span class="keyword">continue</span>; <span class="comment">//在这里注意到 如果是3的倍数 那么不会输出内容</span></span><br><span class="line">  &#125;</span><br><span class="line">   <span class="built_in">document</span>.write(<span class="string">&#x27;正在执行！&#x27;</span>)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>


        <h3 id="typeof-操作符"   >
          <a href="#typeof-操作符" class="heading-link"><i class="fas fa-link"></i></a><a href="#typeof-操作符" class="headerlink" title="typeof 操作符"></a>typeof 操作符</h3>
      <p><strong>在不知道对应实例是属于什么类型的变量时</strong> </p>
<p>我们使用<strong>typeof instance</strong> 来查看对应的变量类型：</p>
<figure class="highlight js"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"><span class="keyword">typeof</span> <span class="string">&quot;john&quot;</span> <span class="comment">//string</span></span><br><span class="line">    </span><br><span class="line"><span class="keyword">typeof</span> <span class="number">3.14</span> <span class="comment">//number</span></span><br><span class="line">    </span><br><span class="line"><span class="keyword">typeof</span> <span class="literal">false</span> <span class="comment">//boolean</span></span><br><span class="line">    </span><br><span class="line"><span class="keyword">typeof</span> [<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>] <span class="comment">//object</span></span><br><span class="line">    </span><br><span class="line"><span class="keyword">typeof</span> &#123;<span class="attr">name</span>:<span class="string">&#x27;John&#x27;</span>, <span class="attr">age</span>:<span class="number">34</span>&#125;<span class="comment">//object</span></span><br><span class="line">    </span><br></pre></td></tr></table></div></figure>


        <h3 id="null对象"   >
          <a href="#null对象" class="heading-link"><i class="fas fa-link"></i></a><a href="#null对象" class="headerlink" title="null对象"></a>null对象</h3>
      <p><strong>在Javascript中，null表示空对象引用</strong></p>
<p>var person = null //即表示该对象是空对象</p>

        <h3 id="undefined（赋这个值以清空某个变量）"   >
          <a href="#undefined（赋这个值以清空某个变量）" class="heading-link"><i class="fas fa-link"></i></a><a href="#undefined（赋这个值以清空某个变量）" class="headerlink" title="undefined（赋这个值以清空某个变量）"></a>undefined（赋这个值以清空某个变量）</h3>
      <p><strong>在JavaScript中 undefined是一个没有设置值的变量</strong></p>

        <h3 id="JavaScript-类型转换"   >
          <a href="#JavaScript-类型转换" class="heading-link"><i class="fas fa-link"></i></a><a href="#JavaScript-类型转换" class="headerlink" title="JavaScript 类型转换"></a>JavaScript 类型转换</h3>
      
        <h4 id="将数字转换为字符串"   >
          <a href="#将数字转换为字符串" class="heading-link"><i class="fas fa-link"></i></a><a href="#将数字转换为字符串" class="headerlink" title="将数字转换为字符串"></a>将数字转换为字符串</h4>
      <figure class="highlight js"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">String</span>(number + number2)</span><br><span class="line"></span><br><span class="line">(number1 + number2).toString()</span><br></pre></td></tr></table></div></figure>



        <h4 id="字符串转换为数字"   >
          <a href="#字符串转换为数字" class="heading-link"><i class="fas fa-link"></i></a><a href="#字符串转换为数字" class="headerlink" title="字符串转换为数字"></a>字符串转换为数字</h4>
      <figure class="highlight js"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">Number</span>(str)</span><br></pre></td></tr></table></div></figure>


        <h3 id="JavaScript-正则表达式"   >
          <a href="#JavaScript-正则表达式" class="heading-link"><i class="fas fa-link"></i></a><a href="#JavaScript-正则表达式" class="headerlink" title="JavaScript 正则表达式"></a>JavaScript 正则表达式</h3>
      <p><strong>正则表达式(Regular Expression) 使用若干个字符来描述：满足特定句法规则的字符串模式</strong></p>

        <h4 id="使用test（）方法"   >
          <a href="#使用test（）方法" class="heading-link"><i class="fas fa-link"></i></a><a href="#使用test（）方法" class="headerlink" title="使用test（）方法"></a>使用test（）方法</h4>
      <p><strong>对于特定的正则表达式，我们可以使用test（）方法来验证某字符串变量是否满足这个特定正则表达式</strong></p>
<figure class="highlight js"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> reg = <span class="regexp">/xxx/</span>;</span><br><span class="line"></span><br><span class="line"><span class="built_in">console</span>.log(reg.test(strValue))</span><br></pre></td></tr></table></div></figure>

<p><strong>若满足这个特定的正则表达式 则输出true 否则输出false</strong></p>

        <h3 id="变量提升"   >
          <a href="#变量提升" class="heading-link"><i class="fas fa-link"></i></a><a href="#变量提升" class="headerlink" title="变量提升"></a>变量提升</h3>
      <p><strong>所谓变量提升，是指函数以及变量的生命都将被提升到函数的最顶部</strong></p>
<p>也就是说，变量可以先使用再声明</p>
<p>但是！<strong>变量的初始化，并不会提升</strong></p>
<figure class="highlight js"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> x = <span class="number">5</span>;</span><br><span class="line">elem = <span class="built_in">document</span>.getElementById(<span class="string">&quot;demo&quot;</span>);</span><br><span class="line">elem.innerHTML = x + <span class="string">&quot;,&quot;</span> + y;</span><br><span class="line"></span><br><span class="line"><span class="keyword">var</span> y = <span class="number">7</span>;</span><br></pre></td></tr></table></div></figure>

<p><strong>在以上例子中，y的值会输出undefined，而这是因为</strong> </p>
<p><em>变量y的初始化在下方，而初始化是不会提升的</em> </p>
<p><strong>因此，我们在敲代码时，要记住，最好是直接在头部声明我们的变量</strong></p>

        <h3 id="块级作用域-let-与常量符-const"   >
          <a href="#块级作用域-let-与常量符-const" class="heading-link"><i class="fas fa-link"></i></a><a href="#块级作用域-let-与常量符-const" class="headerlink" title="块级作用域 let 与常量符 const"></a>块级作用域 let 与常量符 const</h3>
      <p><strong>ES6 可以使用let来使用块级变量符，也就是说 let变量在当前所在的代码块中有效</strong></p>
<p>而在{}外，是无法访问的！</p>
<p><em>对比var 变量符</em></p>
<figure class="highlight js"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">&#123;</span><br><span class="line">	<span class="function"><span class="keyword">function</span> <span class="title">funName</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">		<span class="keyword">var</span> test  = <span class="number">1</span>;</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	<span class="built_in">console</span>.log(test) <span class="comment">//值仍为1  可以访问（此时不具备块级变量的概念）</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>

<figure class="highlight js"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">&#123;</span><br><span class="line">	<span class="function"><span class="keyword">function</span> <span class="title">funName</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">		<span class="keyword">let</span> test = <span class="number">2</span>;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="built_in">console</span>.log(test) <span class="comment">//错误，此时具有块级变量的概念，是不能访问到的</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>

<p>CONST 常量名称</p>
<p><strong>const 定义常量的值必须初始化 因为后期是不能改变的</strong></p>
<p><strong>一旦初始化了 后期是绝对不能改变的</strong></p>
<p><strong>CONST 定义的数组或者对象 里面的属性是可以修改的 但是</strong></p>
<p>我们是不能对这个对象 或者数组重新赋值的。</p>
<figure class="highlight js"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> car = &#123;type=<span class="string">&quot;fia&quot;</span>, <span class="attr">model</span>:<span class="string">&quot;500&quot;</span>, <span class="attr">color</span>:<span class="string">&quot;white&quot;</span>&#125;</span><br><span class="line"></span><br><span class="line">car.color = <span class="string">&quot;red&quot;</span>; <span class="comment">//不会报错的</span></span><br></pre></td></tr></table></div></figure>


        <h3 id="JavaScript-JSON"   >
          <a href="#JavaScript-JSON" class="heading-link"><i class="fas fa-link"></i></a><a href="#JavaScript-JSON" class="headerlink" title="JavaScript JSON"></a>JavaScript JSON</h3>
      <p><strong>JSON的全称是 JavaScript Object Notation</strong></p>
<p>是一种轻量级的数据交换格式，通常用于服务端向网页传递数据</p>
<p>例如：</p>
<figure class="highlight js"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">&#123;</span><br><span class="line">	<span class="string">&quot;sites&quot;</span>:</span><br><span class="line">	[</span><br><span class="line">		&#123;</span><br><span class="line">			<span class="string">&quot;name&quot;</span>: <span class="string">&quot;Google&quot;</span>, </span><br><span class="line">			<span class="string">&quot;url&quot;</span> : <span class="string">&quot;www.google.com&quot;</span></span><br><span class="line">		&#125;,</span><br><span class="line">		&#123;</span><br><span class="line">			<span class="string">&quot;name&quot;</span>: <span class="string">&quot;Baidu&quot;</span>,</span><br><span class="line">			<span class="string">&quot;url&quot;</span>: <span class="string">&quot;www.baidu.com&quot;</span></span><br><span class="line">		&#125;</span><br><span class="line">	]</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>

<ol>
<li><p>数据为键:值对的集合：”key”:”value”</p>
</li>
<li><p>数据与数据之间 用逗号隔开</p>
</li>
<li><p>花括号保存对象，方括号保存数组</p>
</li>
</ol>

        <h4 id="使用内置函数将JSON-字符串转换为JavaScript对象"   >
          <a href="#使用内置函数将JSON-字符串转换为JavaScript对象" class="heading-link"><i class="fas fa-link"></i></a><a href="#使用内置函数将JSON-字符串转换为JavaScript对象" class="headerlink" title="使用内置函数将JSON 字符串转换为JavaScript对象"></a>使用内置函数将JSON 字符串转换为JavaScript对象</h4>
      <figure class="highlight js"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> text = <span class="string">&#x27;&#123; &quot;sites&quot; : [&#x27;</span> +</span><br><span class="line">    <span class="string">&#x27;&#123; &quot;name&quot;:&quot;Runoob&quot; , &quot;url&quot;:&quot;www.runoob.com&quot; &#125;,&#x27;</span> +</span><br><span class="line">    <span class="string">&#x27;&#123; &quot;name&quot;:&quot;Google&quot; , &quot;url&quot;:&quot;www.google.com&quot; &#125;,&#x27;</span> +</span><br><span class="line">    <span class="string">&#x27;&#123; &quot;name&quot;:&quot;Taobao&quot; , &quot;url&quot;:&quot;www.taobao.com&quot; &#125; ]&#125;&#x27;</span>;</span><br><span class="line">	</span><br><span class="line"><span class="keyword">var</span> obj = <span class="built_in">JSON</span>.parse(text);</span><br><span class="line"></span><br></pre></td></tr></table></div></figure>

<p>**JSON.stringify(obj)**是一个相反的过程</p>

        <h3 id="Promise-类解决前端异步调用后台数据"   >
          <a href="#Promise-类解决前端异步调用后台数据" class="heading-link"><i class="fas fa-link"></i></a><a href="#Promise-类解决前端异步调用后台数据" class="headerlink" title="Promise 类解决前端异步调用后台数据"></a>Promise 类解决前端异步调用后台数据</h3>
      <ol>
<li>小例子<figure class="highlight js"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//Promise 的构造函数的参数只有一个</span></span><br><span class="line"></span><br><span class="line"><span class="comment">//就是一个函数 在这个函数中，我们编写要执行的《异步函数代码块》</span></span><br><span class="line"><span class="keyword">new</span> <span class="built_in">Promise</span>(<span class="function"><span class="keyword">function</span> (<span class="params">resolve, reject</span>)</span>&#123;</span><br><span class="line">	<span class="built_in">console</span>.log(<span class="string">&#x27;异步代码块！&#x27;</span>)</span><br><span class="line">&#125;);</span><br></pre></td></tr></table></div></figure></li>
</ol>
<p><strong>其中resolve代表一切正常(译为解决)，reject代表出现异常时调用（译为拒绝）</strong></p>
<p>比如出现零除异常。</p>
<figure class="highlight js"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">new</span> <span class="built_in">Promise</span>(<span class="function"><span class="keyword">function</span> (<span class="params">resolve, reject</span>) </span>&#123;</span><br><span class="line">	<span class="keyword">var</span> a = <span class="number">0</span>;</span><br><span class="line">	<span class="keyword">var</span> b = <span class="number">1</span>;</span><br><span class="line">	<span class="keyword">if</span> (b == <span class="number">0</span>) reject(<span class="string">&quot;Divide zero&quot;</span>);</span><br><span class="line">	<span class="keyword">else</span> resolve(a / b);</span><br><span class="line">&#125;).then(<span class="function"><span class="keyword">function</span> (<span class="params">value</span>) </span>&#123;</span><br><span class="line">	<span class="built_in">console</span>.log(<span class="string">&quot;a / b = &quot;</span> + value);</span><br><span class="line">&#125;).catch(<span class="function"><span class="keyword">function</span> (<span class="params">err</span>) </span>&#123;</span><br><span class="line">	<span class="built_in">console</span>.log(err);</span><br><span class="line">&#125;).finally(<span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">	<span class="built_in">console</span>.log(<span class="string">&quot;End&quot;</span>);</span><br><span class="line">&#125;);</span><br></pre></td></tr></table></div></figure>

<p><strong>我们这样子执行的原因。 虽然执行这些代码块是异步的，但是我们利用then的先后顺序，让它看起来是相对“同步”的</strong></p>
</div></div></article><article class="postlist-item post"><header class="post-header"><h1 class="post-title"><a class="post-title__link" href="/blog/2021/03/15/CSS/">CSS学习笔记</a></h1><div class="post-meta"><span class="post-meta-item post-meta-item--createtime"><span class="post-meta-item__icon"><i class="far fa-calendar-plus"></i></span><span class="post-meta-item__info">发表于</span><span class="post-meta-item__value">2021-03-15</span></span><span class="post-meta-item post-meta-item--updatetime"><span class="post-meta-item__icon"><i class="far fa-calendar-check"></i></span><span class="post-meta-item__info">更新于</span><span class="post-meta-item__value">2021-04-30</span></span><span class="post-meta-item post-meta-item--wordcount"><span class="post-meta-item__icon"><i class="far fa-file-word"></i></span><span class="post-meta-item__info">字数统计</span><span class="post-meta-item__value">2.4k</span></span><span class="post-meta-item post-meta-item--readtime"><span class="post-meta-item__icon"><i class="far fa-clock"></i></span><span class="post-meta-item__info">阅读时长</span><span class="post-meta-item__value">16分</span></span></div></header><div class="post-body"><div class="post-excerpt"><!-- 页面必备样式 -->
<style>
.back-top{position: fixed;top: 500px;height: 0px;float: right;right: 50px;}
.back-top>a{font-weight:bold;width: 50px;height: 50px;text-align: center;text-decoration: none;font-size: 5px;line-height: 50px;border-radius: 50px;background-color:rgba(255,255,255,0.5);}
</style>
<div class="back-top">
    <a href="#top"> >返回顶部< </a>
</div>
<!-- toc -->
<!-- 必备样式结束 -->


        <h1 id="CSS教程"   >
          <a href="#CSS教程" class="heading-link"><i class="fas fa-link"></i></a><a href="#CSS教程" class="headerlink" title="CSS教程"></a>CSS教程</h1>
      
        <h2 id="什么是CSS？"   >
          <a href="#什么是CSS？" class="heading-link"><i class="fas fa-link"></i></a><a href="#什么是CSS？" class="headerlink" title="什么是CSS？"></a>什么是CSS？</h2>
      <ol>
<li><p>CSS 指的是层叠样式表<strong>Cascading Style Sheets</strong></p>
</li>
<li><p>样式通常存储在.css文件中 解决内容与样式定义的问题</p>
</li>
</ol>

        <h2 id="CSS-标准语法"   >
          <a href="#CSS-标准语法" class="heading-link"><i class="fas fa-link"></i></a><a href="#CSS-标准语法" class="headerlink" title="CSS 标准语法"></a>CSS 标准语法</h2>
      <p><strong>通常，CSS规则由 选择器，若干条声明组成</strong></p>
<p>比如：</p>
<figure class="highlight css"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="selector-tag">h1</span>&#123; <span class="attribute">color</span>:blur; <span class="attribute">font-size</span>:<span class="number">12px</span>; &#125;</span><br></pre></td></tr></table></div></figure>
<p>h1 是选择器，选择对应的标签。</p>
<p>花括号中是若干条声明，其中每个样式由<strong>属性：值</strong>构成。</p>

        <h2 id="CSS-注释"   >
          <a href="#CSS-注释" class="heading-link"><i class="fas fa-link"></i></a><a href="#CSS-注释" class="headerlink" title="CSS 注释"></a>CSS 注释</h2>
      <figure class="highlight css"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/*这是一个注释内容*/</span></span><br></pre></td></tr></table></div></figure>


        <h2 id="CSS-ID选择器与CLASS选择器"   >
          <a href="#CSS-ID选择器与CLASS选择器" class="heading-link"><i class="fas fa-link"></i></a><a href="#CSS-ID选择器与CLASS选择器" class="headerlink" title="CSS ID选择器与CLASS选择器"></a>CSS ID选择器与CLASS选择器</h2>
      <p><strong>ID选择器具有唯一性，CLASS类选择器将样式应用在同一类名的标签上</strong></p>

        <h3 id="ID选择器"   >
          <a href="#ID选择器" class="heading-link"><i class="fas fa-link"></i></a><a href="#ID选择器" class="headerlink" title="ID选择器"></a>ID选择器</h3>
      <figure class="highlight css"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="selector-id">#id</span>&#123;</span><br><span class="line">	attr: value;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>


        <h3 id="Class选择器"   >
          <a href="#Class选择器" class="heading-link"><i class="fas fa-link"></i></a><a href="#Class选择器" class="headerlink" title="Class选择器"></a>Class选择器</h3>
      <figure class="highlight css"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="selector-class">.class</span>&#123;</span><br><span class="line">	attr: value;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>


        <h2 id="CSS-引入"   >
          <a href="#CSS-引入" class="heading-link"><i class="fas fa-link"></i></a><a href="#CSS-引入" class="headerlink" title="CSS 引入"></a>CSS 引入</h2>
      <ol>
<li>外部样式表</li>
</ol>
<p><strong>我们一般在网页的头部<code>&lt;head&gt;</code>标签插入外部样式表</strong></p>
<figure class="highlight css"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">&lt;link rel=&quot;stylesheet&quot; type=&quot;text/css&quot; href=&quot;cssName<span class="selector-class">.css</span>&quot;&gt;</span><br></pre></td></tr></table></div></figure>

<ol start="2">
<li>内部样式表</li>
</ol>
<p><strong>在网页的最底部区域，若文档需要特定的样式，则使用<code>&lt;style&gt;</code>标签定义样式</strong></p>
<figure class="highlight css"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">&lt;style&gt;</span><br><span class="line">	<span class="selector-tag">p</span>&#123;</span><br><span class="line">		<span class="attribute">margin-left</span>: <span class="number">20px</span>;</span><br><span class="line">	&#125;</span><br><span class="line">&lt;/style&gt;</span><br></pre></td></tr></table></div></figure>

<ol start="3">
<li>内联样式</li>
</ol>
<p><strong>对某个元素使用style属性，即可使用内联样式。</strong></p>
<figure class="highlight css"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">&lt;<span class="selector-tag">p</span> style=&quot; <span class="attribute">color</span>:blue; <span class="attribute">background-color</span>:red; &quot;&gt;</span><br><span class="line">	这是段落内容...</span><br><span class="line">  </span><br></pre></td></tr></table></div></figure>

<ul>
<li>多重样式优先级：内联样式 &gt; 内部样式 &gt; 外部样式</li>
</ul>

        <h2 id="CSS-背景"   >
          <a href="#CSS-背景" class="heading-link"><i class="fas fa-link"></i></a><a href="#CSS-背景" class="headerlink" title="CSS 背景"></a>CSS 背景</h2>
      <ol>
<li>background-color 背景颜色</li>
<li>background-image 背景图片</li>
<li>background-repeat 背景图片是否、如何重复</li>
<li>background-attachment 背景图像是否固定或者随着页面滚动</li>
<li>background-position 背景图像的初始位置</li>
</ol>

        <h2 id="CSS-文本"   >
          <a href="#CSS-文本" class="heading-link"><i class="fas fa-link"></i></a><a href="#CSS-文本" class="headerlink" title="CSS 文本"></a>CSS 文本</h2>
      <ol>
<li>文本颜色 color</li>
</ol>
<p><strong>文本颜色一般指定为：十六进制值#FF0000 RGB值 RGB(255, 0, 0) 或者名称 red</strong></p>
<ol start="2">
<li>文本对齐方式 text-align</li>
</ol>
<p><strong>center代表居中显示，justify像报纸一样，左右边距对齐，每一行宽度相等，right右对齐。</strong></p>
<ol start="3">
<li>文本修饰 text-decoration</li>
</ol>
<p><strong>none代表不需要装饰，overline上划线，line-through删除线，underline下划线。</strong></p>
<ol start="4">
<li>文本转换属性 text-transform</li>
</ol>
<p><strong>uppercase全部大写，lowercase全部小写，capitalize首字母大写。</strong></p>
<ol start="5">
<li>文本缩进 text-indent</li>
</ol>
<p><strong>px像素点为基本单位</strong></p>
<ol start="6">
<li>文本阴影 text-shadow</li>
</ol>
<p><strong>上右下左为基本方向，px像素点为基本单位。</strong></p>
<figure class="highlight css"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="selector-tag">p</span>&#123;</span><br><span class="line">	<span class="comment">/* 表示上下为2px 左右为2px 颜色是#FF0000 */</span></span><br><span class="line">	<span class="attribute">text-shadow</span>: <span class="number">2px</span> <span class="number">2px</span> <span class="number">#FF0000</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>


        <h2 id="CSS-字体"   >
          <a href="#CSS-字体" class="heading-link"><i class="fas fa-link"></i></a><a href="#CSS-字体" class="headerlink" title="CSS 字体"></a>CSS 字体</h2>
      <ol>
<li><p>多种字体设置 font-family</p>
<figure class="highlight css"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/*如果前面的字体 不起作用 往后替换*/</span></span><br><span class="line">	<span class="attribute">font-family</span>:<span class="string">&quot;Times New Roman&quot;</span>, Times, serif;</span><br></pre></td></tr></table></div></figure></li>
<li><p>字体样式 font-style</p>
</li>
</ol>
<p><strong>normal代表正常，italic代表倾斜。</strong></p>
<p><strong>p.s. 在font-weight:bold;中设置字体为粗体效果</strong></p>
<ol start="3">
<li>字体大小 font-size</li>
</ol>
<figure class="highlight css"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="selector-tag">p</span>&#123;</span><br><span class="line">	<span class="attribute">font-size</span>: <span class="number">12px</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>


        <h2 id="CSS-链接"   >
          <a href="#CSS-链接" class="heading-link"><i class="fas fa-link"></i></a><a href="#CSS-链接" class="headerlink" title="CSS 链接"></a>CSS 链接</h2>
      <figure class="highlight css"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="selector-tag">a</span><span class="selector-pseudo">:link</span> &#123;<span class="attribute">color</span>:<span class="number">#000000</span>;&#125;      <span class="comment">/* 未访问链接*/</span></span><br><span class="line"><span class="selector-tag">a</span><span class="selector-pseudo">:visited</span> &#123;<span class="attribute">color</span>:<span class="number">#00FF00</span>;&#125;  <span class="comment">/* 已访问链接 */</span></span><br><span class="line"><span class="selector-tag">a</span><span class="selector-pseudo">:hover</span> &#123;<span class="attribute">color</span>:<span class="number">#FF00FF</span>;&#125;  <span class="comment">/* 鼠标移动到链接上 */</span></span><br><span class="line"><span class="selector-tag">a</span><span class="selector-pseudo">:active</span> &#123;<span class="attribute">color</span>:<span class="number">#0000FF</span>;&#125;  <span class="comment">/* 鼠标点击时 */</span></span><br></pre></td></tr></table></div></figure>
<ul>
<li>a:hover必须跟在a:link与a:visited后面</li>
<li>a:active必须跟在a:hover后面</li>
</ul>

        <h2 id="CSS-列表"   >
          <a href="#CSS-列表" class="heading-link"><i class="fas fa-link"></i></a><a href="#CSS-列表" class="headerlink" title="CSS 列表"></a>CSS 列表</h2>
      <p><strong>在HTML中，有两种类型的列表：</strong></p>
<ul>
<li>无序列表 ul 小黑点或正方块</li>
<li>有序列表 ol 数字或字母</li>
</ul>
<figure class="highlight css"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="selector-tag">ul</span><span class="selector-class">.a</span>&#123;</span><br><span class="line">	<span class="attribute">list-style-type</span>: circle;</span><br><span class="line">&#125;</span><br><span class="line"><span class="selector-tag">ul</span><span class="selector-class">.b</span>&#123;</span><br><span class="line">	<span class="attribute">list-style-type</span>:square;</span><br><span class="line">&#125;</span><br><span class="line"><span class="selector-tag">ol</span><span class="selector-class">.c</span>&#123;</span><br><span class="line">	<span class="attribute">list-style-type</span>:upper-roman;</span><br><span class="line">&#125;</span><br><span class="line"><span class="selector-tag">ol</span><span class="selector-class">.d</span>&#123;</span><br><span class="line">	<span class="attribute">list-style-type</span>:lower-alpha;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>

<p><strong>如果要去掉列表的样式，可以使用</strong></p>
<figure class="highlight css"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="selector-tag">ul</span><span class="selector-class">.demo</span>:&#123;</span><br><span class="line">	list-style-type:none;</span><br><span class="line">	<span class="attribute">margin</span>:<span class="number">0px</span>;</span><br><span class="line">	<span class="attribute">padding</span>:<span class="number">0px</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>


        <h2 id="CSS-表格"   >
          <a href="#CSS-表格" class="heading-link"><i class="fas fa-link"></i></a><a href="#CSS-表格" class="headerlink" title="CSS 表格"></a>CSS 表格</h2>
      <ol>
<li><p>表格边框 border: 1px solid black; </p>
</li>
<li><p>表格边框折叠成单一边框 border-collapse:collapse;</p>
</li>
<li><p>表格文字对齐 td{text-align:center}</p>
</li>
</ol>

        <h2 id="CSS-盒子模型"   >
          <a href="#CSS-盒子模型" class="heading-link"><i class="fas fa-link"></i></a><a href="#CSS-盒子模型" class="headerlink" title="CSS 盒子模型"></a>CSS 盒子模型</h2>
      <p><strong>所有HTML元素都可以看作是一个盒子，在CSS中，这些元素包括：</strong></p>
<p><strong>边距，边框，填充，实际内容</strong></p>
<p><img   src="https://img-blog.csdnimg.cn/20210430155052815.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L20wXzQzNDQ1MDY3,size_16,color_FFFFFF,t_70" style="width: image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,tepx;height: t_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L20wXzQzNDQ1MDY3,size_16,color_FFFFFF,t_70px;"  alt="盒子模型"></p>
<p>其中：</p>
<ol>
<li><p>Margin 外边距</p>
</li>
<li><p>Border 边框</p>
</li>
<li><p>Padding 内边距</p>
</li>
<li><p>Content 内容</p>
</li>
</ol>

        <h2 id="CSS-边框"   >
          <a href="#CSS-边框" class="heading-link"><i class="fas fa-link"></i></a><a href="#CSS-边框" class="headerlink" title="CSS 边框"></a>CSS 边框</h2>
      <ol>
<li><p>边框宽度 border-width</p>
</li>
<li><p>边框样式 border-style</p>
</li>
</ol>
<p><strong>dotted 点线边框， dashed 虚线边框， solid实线边框。</strong></p>
<ol start="3">
<li>边框颜色 border-color</li>
</ol>
<p>还是那三种方法，RGB，name，十六进制</p>
<ol start="4">
<li>边框圆角 border-radius:50px;</li>
</ol>

        <h2 id="CSS-外边距-Margin"   >
          <a href="#CSS-外边距-Margin" class="heading-link"><i class="fas fa-link"></i></a><a href="#CSS-外边距-Margin" class="headerlink" title="CSS 外边距 Margin"></a>CSS 外边距 Margin</h2>
      <p>CSS中的外边距指的是元素周围的空间</p>
<p><img   src="https://img-blog.csdnimg.cn/20210430155755277.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L20wXzQzNDQ1MDY3,size_16,color_FFFFFF,t_70" style="width: image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,tepx;height: t_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L20wXzQzNDQ1MDY3,size_16,color_FFFFFF,t_70px;"  alt="外边距与内边距"></p>
<p>可单独调整元素的各种方向的外边距：</p>
<figure class="highlight css"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="attribute">margin-top</span>: <span class="number">20px</span>;</span><br><span class="line"><span class="attribute">margin-right</span>: <span class="number">20px</span>;</span><br><span class="line"><span class="attribute">margin-bottom</span>: <span class="number">20px</span>;</span><br><span class="line"><span class="attribute">margin-left</span>: <span class="number">20px</span>;</span><br></pre></td></tr></table></div></figure>

        <h2 id="CSS-填充边距（内边距）"   >
          <a href="#CSS-填充边距（内边距）" class="heading-link"><i class="fas fa-link"></i></a><a href="#CSS-填充边距（内边距）" class="headerlink" title="CSS 填充边距（内边距）"></a>CSS 填充边距（内边距）</h2>
      <p><strong>与外边距一样，内边距也遵循：上右下左的样式顺序</strong></p>

        <h2 id="分组与嵌套选择器"   >
          <a href="#分组与嵌套选择器" class="heading-link"><i class="fas fa-link"></i></a><a href="#分组与嵌套选择器" class="headerlink" title="分组与嵌套选择器"></a>分组与嵌套选择器</h2>
      <p><strong>有可能，我们想对多个元素或者区域使用同样的样式。</strong></p>
<figure class="highlight css"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="selector-tag">p</span>&#123;</span><br><span class="line">	</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="selector-class">.className</span>&#123;</span><br><span class="line">	</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>

<ol>
<li><p>用逗号将同样样式的元素写上</p>
<figure class="highlight css"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="selector-tag">p</span>,<span class="selector-class">.className</span>&#123;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure></li>
<li><p>空格选择包裹在内的元素样式</p>
</li>
</ol>
<p><strong>为所有class=”className”元素内的p元素指定一个样式</strong></p>
<figure class="highlight css"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="selector-class">.className</span> <span class="selector-tag">p</span>&#123;</span><br><span class="line">	</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>

<ol start="3">
<li>不用空格隔开，选择符合多种条件的元素</li>
</ol>
<p><strong>为所有class=”className”的p元素指定一个样式</strong></p>
<figure class="highlight css"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="selector-tag">p</span><span class="selector-class">.className</span>&#123;</span><br><span class="line">	</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>

        <h3 id="组合选择符"   >
          <a href="#组合选择符" class="heading-link"><i class="fas fa-link"></i></a><a href="#组合选择符" class="headerlink" title="组合选择符"></a>组合选择符</h3>
      <ol>
<li>后代选择器 空格隔开</li>
<li>子元素选择器 &gt; 符号</li>
<li>相邻兄弟选择器 + </li>
<li>普通兄弟 ～</li>
</ol>

        <h2 id="CSS-尺寸"   >
          <a href="#CSS-尺寸" class="heading-link"><i class="fas fa-link"></i></a><a href="#CSS-尺寸" class="headerlink" title="CSS 尺寸"></a>CSS 尺寸</h2>
      <figure class="highlight css"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="selector-tag">p</span>&#123;</span><br><span class="line">	<span class="attribute">line-height</span>: <span class="number">50px</span>; <span class="comment">/*设置行高*/</span></span><br><span class="line">	<span class="attribute">min-height</span>: <span class="number">50px</span>;</span><br><span class="line">	<span class="attribute">max-height</span>: <span class="number">50px</span>;</span><br><span class="line">	<span class="attribute">min-width</span>: <span class="number">50px</span>;</span><br><span class="line">	<span class="attribute">min-height</span>: <span class="number">50px</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>


        <h2 id="Display与visibility"   >
          <a href="#Display与visibility" class="heading-link"><i class="fas fa-link"></i></a><a href="#Display与visibility" class="headerlink" title="Display与visibility"></a>Display与visibility</h2>
      <p><strong>隐藏一个元素可以通过两种方式：1. display:none; 2. visibility:hidden;</strong></p>
<ul>
<li>visibility:hidden; 可以隐藏某个元素，但是这个隐藏的元素不会从DOM中脱离，仍会影响布局。</li>
<li>而display:none; 使得该元素不但被隐藏了，而且这个元素原本占用的空间也会从页面布局中消失。</li>
</ul>

        <h2 id="CSS-块级元素与内联元素"   >
          <a href="#CSS-块级元素与内联元素" class="heading-link"><i class="fas fa-link"></i></a><a href="#CSS-块级元素与内联元素" class="headerlink" title="CSS 块级元素与内联元素"></a>CSS 块级元素与内联元素</h2>
      <p><strong>像 h1, p, div 这些都是块元素，占用了全部宽度，在前后都是换行符</strong></p>
<p><strong>而像span a 这种都是内联元素，不会占用全部宽度，不强制换行。</strong></p>

        <h2 id="CSS-定位"   >
          <a href="#CSS-定位" class="heading-link"><i class="fas fa-link"></i></a><a href="#CSS-定位" class="headerlink" title="CSS 定位"></a>CSS 定位</h2>
      <ol>
<li><p>static 默认定位 遵循正常的文档流对象</p>
</li>
<li><p>fixed 固定位置 即使窗口是滚动的它也不会移动</p>
</li>
<li><p>relative 相对位置 相对其DOM中正常的位置</p>
</li>
<li><p>absolute 绝对定位 相对于最近的已定位父元素 如果没有 就是相对于HTML</p>
</li>
<li><p>cursor:pointer; 光标呈现手的形式</p>
</li>
</ol>

        <h2 id="CSS-布局"   >
          <a href="#CSS-布局" class="heading-link"><i class="fas fa-link"></i></a><a href="#CSS-布局" class="headerlink" title="CSS 布局"></a>CSS 布局</h2>
      <p><strong>我们使用overflow来控制内容溢出元素框时，对应元素的显示情况。</strong></p>
<ol>
<li><p>overflow: visible; 内容不会修剪，会呈现在元素框之外。</p>
</li>
<li><p>hidden; 内容会被修剪，并且其余内容是不可见的。</p>
</li>
<li><p>scroll; 内容会被修剪，但是浏览器会显示滚动条以便查看其余的内容。</p>
</li>
<li><p>auto; 如果被修剪，会显示滚动条查看其余的内容。</p>
</li>
</ol>

        <h2 id="CSS-浮动"   >
          <a href="#CSS-浮动" class="heading-link"><i class="fas fa-link"></i></a><a href="#CSS-浮动" class="headerlink" title="CSS 浮动"></a>CSS 浮动</h2>
      <p><strong>浮动会使得元素，向左或向右移动，剩下的元素也会重新排列；</strong></p>
<p><strong>这对布局图片时尤为重要。</strong>  </p>
<p><strong>水平浮动使得元素只能左右移动，如果图像是向右浮动，那么其余的文本流则会左环绕这张图像，浮动取值如下：</strong></p>
<ol>
<li><p>left 元素向左浮动  </p>
</li>
<li><p>right 元素向右浮动  </p>
</li>
<li><p>none 元素不进行浮动  </p>
</li>
</ol>
<p>CSS浮动具有<strong>破坏性与包裹性</strong>的特点；  </p>
<ol>
<li>破坏性： 首先浮动破坏了行高，被浮动的元素不算行高；同时，浮动也引起了父元素坍塌 问题，即当父级元素全部浮动，并且未设置宽度和高度，那么它的高度将会缩减至零。  </li>
<li>包裹性：浮动的元素将会以  <strong>inline-block</strong>  的方式显示，也就是说<strong>这些元素会与其他的元素共用一行。</strong></li>
</ol>
<ul>
<li>如何通过添加属性来取消某一方向或所有方向的浮动呢？</li>
<li><em>清除浮动： (clear: left|right|both) 清除左/右/两者浮动</em>*  </li>
</ul>
<ol>
<li><p>不允许左、右或者两者方向上的浮动  </p>
</li>
<li><p>优先向上浮动  </p>
</li>
<li><p>在position:absolute;前提下，z-index（z坐标）值越大 会越高覆盖在上面  </p>
</li>
</ol>

        <h2 id="CSS-变形"   >
          <a href="#CSS-变形" class="heading-link"><i class="fas fa-link"></i></a><a href="#CSS-变形" class="headerlink" title="CSS 变形"></a>CSS 变形</h2>
      <p><strong>CSS中的常用2D变换translate rotate scale</strong></p>

        <h3 id="平面移位-translate"   >
          <a href="#平面移位-translate" class="heading-link"><i class="fas fa-link"></i></a><a href="#平面移位-translate" class="headerlink" title="平面移位 translate"></a>平面移位 translate</h3>
      <p>平面移位是根据元素的坐标进行移动，从该位置进行移动，（x，y）表示 </p>
<p>从该元素的位置起，<strong>水平移动x个像素点，垂直移动y个像素点</strong></p>
<p>值得注意的是，由于坐标点在网页的左上角，<strong>因此可以通过负值往反方向平移。</strong></p>
<p>实例：（平面移动元素） </p>
<figure class="highlight css"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="selector-tag">div</span>&#123;  </span><br><span class="line">	<span class="attribute">transform</span>: <span class="built_in">translate</span>(<span class="number">50px</span>,<span class="number">100px</span>);  </span><br><span class="line">	-webkit-<span class="attribute">transform</span>: <span class="built_in">translate</span>(<span class="number">50px</span>,<span class="number">100px</span>); <span class="comment">/*针对于Safari和google浏览器*/</span>   </span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>


        <h3 id="旋转-rotate"   >
          <a href="#旋转-rotate" class="heading-link"><i class="fas fa-link"></i></a><a href="#旋转-rotate" class="headerlink" title="旋转 rotate"></a>旋转 rotate</h3>
      <p><strong>rotate(x)通过给定的度数x，让元素进行顺时针旋转，x可取负值以看做是逆时针旋转。</strong></p>
<ul>
<li>实例：（旋转元素）  <figure class="highlight css"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="selector-tag">div</span>&#123;  </span><br><span class="line">	<span class="attribute">transform</span>: <span class="built_in">rotate</span>(<span class="number">50px</span>,<span class="number">100px</span>);  </span><br><span class="line">	-webkit-<span class="attribute">transform</span>: <span class="built_in">rotate</span>(<span class="number">50px</span>,<span class="number">100px</span>); <span class="comment">/*针对于Safari和google浏览器*/</span>   </span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure></li>
</ul>

        <h3 id="缩放-scale"   >
          <a href="#缩放-scale" class="heading-link"><i class="fas fa-link"></i></a><a href="#缩放-scale" class="headerlink" title="缩放 scale"></a>缩放 scale</h3>
      <p><strong>scale（x,y) 使得变形后的宽度是之前的x倍，同时高度是之前的y倍。</strong></p>
<ul>
<li>实例：（缩放元素）  <figure class="highlight css"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="selector-tag">div</span>&#123;  </span><br><span class="line">	<span class="attribute">transform</span>: <span class="built_in">scale</span>(<span class="number">2</span>,<span class="number">3</span>);   </span><br><span class="line">	-webkit-<span class="attribute">transform</span>: <span class="built_in">scale</span>(<span class="number">2</span>,<span class="number">3</span>);  <span class="comment">/*针对于Safari和google浏览器*/</span>   </span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>

        <h2 id="CSS-动画"   >
          <a href="#CSS-动画" class="heading-link"><i class="fas fa-link"></i></a><a href="#CSS-动画" class="headerlink" title="CSS 动画"></a>CSS 动画</h2>
      </li>
</ul>

        <h3 id="keyframes-规则"   >
          <a href="#keyframes-规则" class="heading-link"><i class="fas fa-link"></i></a><a href="#keyframes-规则" class="headerlink" title="@keyframes 规则"></a>@keyframes 规则</h3>
      <p><strong>@keyframes 是动画规则，该规则内指定从现有的状态到指定的状态，这个状态间的改变过程就成为动画。</strong></p>
<ul>
<li>实例：制作div元素背景色，从红到黄持续4s（如何自定义动画规则，并将该规则绑定在元素身上）</li>
</ul>
<figure class="highlight css"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">@keyframes</span> test   </span><br><span class="line">&#123;   </span><br><span class="line">	<span class="selector-tag">from</span> &#123;<span class="attribute">background</span>: red;&#125;   </span><br><span class="line">	<span class="selector-tag">to</span> &#123;<span class="attribute">background</span>: yellow;&#125;   </span><br><span class="line">&#125;   </span><br><span class="line"></span><br><span class="line"><span class="keyword">@-webkit-keyframes</span> test  /*safari浏览器与google浏览器的版本号*/    </span><br><span class="line">&#123;   </span><br><span class="line">	<span class="selector-tag">from</span> &#123;<span class="attribute">background</span>: red;&#125;   </span><br><span class="line">	<span class="selector-tag">to</span> &#123;<span class="attribute">background</span>: yellow;&#125;   </span><br><span class="line">&#125; </span><br><span class="line"></span><br><span class="line"><span class="selector-tag">div</span>&#123;</span><br><span class="line">	<span class="attribute">animation</span>: test <span class="number">4s</span>;</span><br><span class="line">	-webkit-<span class="attribute">animation</span>: myfirst <span class="number">5s</span>; safari浏览器与google浏览器的版本号</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>

<p><strong>动画常用属性及说明</strong></p>
<div class="table-container"><table>
<thead>
<tr>
<th>属性名</th>
<th>值&amp;属性说明</th>
</tr>
</thead>
<tbody><tr>
<td>animation-name</td>
<td>属性名字</td>
</tr>
<tr>
<td>animation-duration</td>
<td>动画持续时间，单位为秒</td>
</tr>
<tr>
<td>animation-delay</td>
<td>动画延迟时间，单位为秒</td>
</tr>
<tr>
<td>animation-iteration-count</td>
<td>动画循环次数，infine表示无限循环</td>
</tr>
</tbody></table></div>
</div></div></article><article class="postlist-item post"><header class="post-header"><h1 class="post-title"><a class="post-title__link" href="/blog/2021/03/15/MongoDB/">MongoDB学习笔记</a></h1><div class="post-meta"><span class="post-meta-item post-meta-item--createtime"><span class="post-meta-item__icon"><i class="far fa-calendar-plus"></i></span><span class="post-meta-item__info">发表于</span><span class="post-meta-item__value">2021-03-15</span></span><span class="post-meta-item post-meta-item--updatetime"><span class="post-meta-item__icon"><i class="far fa-calendar-check"></i></span><span class="post-meta-item__info">更新于</span><span class="post-meta-item__value">2021-04-18</span></span><span class="post-meta-item post-meta-item--wordcount"><span class="post-meta-item__icon"><i class="far fa-file-word"></i></span><span class="post-meta-item__info">字数统计</span><span class="post-meta-item__value">2.8k</span></span><span class="post-meta-item post-meta-item--readtime"><span class="post-meta-item__icon"><i class="far fa-clock"></i></span><span class="post-meta-item__info">阅读时长</span><span class="post-meta-item__value">18分</span></span></div></header><div class="post-body"><div class="post-excerpt"><!-- 页面必备样式 -->
<style>
.back-top{position: fixed;top: 500px;height: 0px;float: right;right: 50px;}
.back-top>a{font-weight:bold;width: 50px;height: 50px;text-align: center;text-decoration: none;font-size: 5px;line-height: 50px;border-radius: 50px;background-color:rgba(255,255,255,0.5);}
</style>
<div class="back-top">
    <a href="#top"> >返回顶部< </a>
</div>
<!-- toc -->
<!-- 必备样式结束 -->

<!-- 以下是MongoDB的笔记 -->
<div id="mongodb">
<h2>MongoDB<small>非关系数据库中的佼佼者</small></h2>

<blockquote>         
<p>
<i class="fa fa-quote-left"></i>  MongoDB，源自英语“Humongous”巨大的，当今也被广泛应用于大数据；Not only SQL也具体说出了非关系型数据库的心声，为什么呢？
SQL是结构化，关系型的数据库，是数据库的当之无愧的老大：MySQL&SQL Server。这两家数据库在这几年的发展一直高居不下。同时，由
于大数据的蓬勃发展，大量的信息开始趋于非关系化，非结构化，冗杂繁多的信息量使得数据库本身越来越复杂，因此“关系型数据库”的规范
化也越来越明显，同时也需要建立越来越多的表，关联，数据库。
作为NoSQL领军者的Redis和mongoDB后来者居上，MongoDB是一个跨平台，面向文档的数据库，提供高性能，高可用性和易于扩展；同时是工作在集合
和文档上的一种概念。在处理大数据的冗杂性与结构繁多性，非关系型数据库MongoDB将成为数据库中的“秀儿”。
<i class="fa fa-quote-right"></i>
</p>
</blockquote>

<div class="reference clearfix"> 
<div class="title_content" style="margin-bottom:5px">
<div  class="text_content">笔记前言</div>
<div class="clear"></div>
</div>
</div>        

<br>
<!-- 笔记重要概念 -->

<p><em>一：数据库</em><br>MongoDB中的Database是Collection的物理容器，也就是存储所有文件与集合的文件系统，通常在一个MongoDB Server中存在有至少一个的Database。</p>
<p><em>二：集合</em></p>
<p>集合（Collection）可以说是数据库紧接着的下一个重要的概念，同时也是MongoDB的重要的特点之一，可以和RDBMS（Relational Database Management System）中的table等效；也就是说它是许多文件数据的存储点。一个集合存在于数据库中。集合不强制执行模式。集合中的文档可以有不同的字段。通常情况下，在一个集合中的所有文件都是类似或相关目的。因此这也是NoSQL数据库的众多特点与优点之一。</p>
<p class="im_concept">
<em>三：文件</em><br>
&nbsp;&nbsp;&nbsp;&nbsp;文档是一组键值对（Key-Value)。文档具有动态模式。动态模式是指，在同一个集合的文件不必具有相同一组集合的文档字段或结构，并且相同的字段可以保持不同类型的数据。
</p>                                                                     

<!-- 笔记正文 -->
<div class="reference clearfix"> 

<div class="bottom-p">
<!-- 数据库部分 -->
<div class="panel-group" id="accordion">
<div class="panel panel-default">

<div id="mongo_db" class="panel-collapse collapse in">
<div class="panel-body">
<h3>创建数据库</h3>
<h5>相应命令与语法</h5>
<p>
MongoDB建立一个新的数据库会使用到<code>use database_name</code>命令，
如果不存在该database的名字，将会重新创建一个。否则会返回use的这个数据库：使用use命令创建数据库->
<br>
<br>    
<code>db</code>命令查看当前所使用的数据库<br>
<code>
> db;<br>
databasetest <br>
</code>
<br>

<p>MongoDB查看当前所有数据库会使用到<code>show</code>命令:</p>
<blockquote>
<p>show dbs;<br>admin 0.00GB<br>local 0.00GB<br>test  0.00GB </p>
</blockquote>
<br>
在MongoDB中默认的数据库是<code>test</code>，若没有创建过任何数据库则会默认将数据保存在这个数据库中。
<br>
</p>

<h3>删除数据库</h3>
<h5>相应命令与语法</h5>
<p>
MongoDB数据库删除一个当前已有的数据库，将会用到<code>db.dropDatabase()</code>命令，
前面已提到，如果未曾创建过任何数据库，那么将会删除默认的<code>test</code>的数据库；<br>

<blockquote>
<p>db.dropDatabase();<br>{“dropped” : “test”, “ok” : 1}</p>
</blockquote>
</p>
</div>
</div>
</div>
</div>
</div>

<div class="bottom-p">
<!-- 集合部分 -->
<div class="panel-group" id="accordion">
<div class="panel panel-default">
<div id="mongo_col" class="panel-collapse collapse in">
<div class="panel-body">
<h3>创建集合</h3>
<h5>相应命令与语法</h5>

<p>
MongoDB建立一个新的集合会使用到<code>db.createCollection(collection_name,collection_options)</code>命令，
该命令中，collection_name是集合的名字，而collection_options是指定可选的配置属性：
</p>

<p><img   src="https://img-blog.csdnimg.cn/20210317153238734.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L20wXzQzNDQ1MDY3,size_16,color_FFFFFF,t_70" style="width: image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,tepx;height: t_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L20wXzQzNDQ1MDY3,size_16,color_FFFFFF,t_70px;"  alt="在这里插入图片描述"></p>
<p>
<h5><strong>如何在test数据库中创建集合</strong></h5>

<blockquote>
<p>use test<br>switched to db test<br>db.createCollection(“runoob”)<br>{ “ok” : 1 }</p>
</blockquote>
<p>在你插入了一些文档数据后，MongoDB会自动帮你创建集合。</p>
<blockquote>
<p>db.mongo.insert({“name:” “vincent”});<br>show collections;<br>mongo</p>
</blockquote>
<h3>删除集合</h3>
<h5>相关命令与语法</h5>
<p>
MongoDB中删除一个新的集合会使用到<code>db.collection_name.drop()</code>方法<br>
<em>返回值</em><br>
&nbsp;&nbsp;&nbsp;&nbsp;
如果成功删除选定的集合，该方法将会返回true，否则会返回false.<br>
</p>

<p>
<strong>实例</strong> <br>
首先可以使用<code>show collections;</code>查看当前所有已经存在的集合。

<blockquote>
<p>use test;<br>switched to db test<br>show collections;<br>test<br>local </p>
</blockquote>
<p>接着删除该集合</p>
<blockquote>
<p>db.test.drop();<br>true </p>
</blockquote>
<p>通过<code>show collections</code>再次查看数据库test中的所有集合. </p>
<blockquote>
<p>show collections;<br>local </p>
</blockquote>
<p>我们可以看出<mark>test</mark>集合已被我们删除。</p>
</p>
</p>
</div>
</div>
</div>
</div>
</div>

<div class="bottom-p">
<!-- 集合部分 -->
<div class="panel-group" id="accordion">
<div class="panel panel-default">

<div id="mongo_doc" class="panel-collapse collapse in">
<div class="panel-body">
<h3>MongoDB 插入文档<small>相关命令与语法</small></h3>
<p>
MongoDB使用<code>insert()</code>或者<code>save()</code>方法向集合中插入文档，
<mark>就像关系型数据库中，向表中插入数据一样。</mark><br>

<p><strong>实例</strong><br>将数据插入到test集合中</p>
<pre><code>db.test.insert(&#123;
name: &#39;mongodb&#39;,
writer: &#39;vincent&#39;
&#125;);
</code></pre>
<p>当然，也可以使用db.test.save()命令，如果不指定_id字段，方法类似insert()方法。如果指定，则会更新该_id的数据。<br><br><br>在3.2版本后，还有一下几种语法可用于插入文档：</p>
<pre><code>db.collection_name.inertOne();//插入一条数据
db.collection_name.insertMany();//插入多条数据
</code></pre>
</p>
<h3>MongoDB 更新文档<small>相关命令与语法</small></h3>
<p>
MongoDB使用<code>update()</code>与<code>save()</code>更新文档，分别来看看这两个方法：<br>
<h4>update()方法</h4>
<mark>update()方法用于更新已存在的的文档，语法如下：<br></mark>
<br>

<p>db.test.update(<br><strong>query 查找方式</strong>,<br><strong>update 如何操作</strong>,<br>);</p>
<p><strong>实例<br></strong></p>
<p>首先已知在test集合中，插入了name为vincent的数据。现在需要修改为rebecca：</p>
<pre><code>&gt;db.test.update(&#123;&#39;name&#39;:&#39;vincent&#39;&#125;,&#123;$set:&#123;&#39;name&#39;:&#39;rebecca&#39;&#125;&#125;);
WriteResult(&#123; &quot;nMatched&quot; : 1, &quot;nUpserted&quot; : 0, &quot;nModified&quot; : 1 &#125;) 
</code></pre>
<h4>save()方法</h4>
<mark>save()方法通过传入的文档来替换已有文档。语法如下：<br></mark>


<pre><code>db.test.save(
document
)
</code></pre>
</p>

<h3>MongoDB 删除文档<small>相关命令与语法</small></h3>
<p>
MongoDB中删除文档，将会使用到<code>remove()</code>方法，建议在删除之前，先查询一下想要删除的数据对象详情。<br><br>

<pre><code>db.test.remove(query,&#123;justOne: boolean;&#125;);
</code></pre>
<p><ins>其中，justOne是可选的，它代表是否只删除，多个符合删除条件中的，一条数据。如果选择只删除一条，那么置为1（true）。</ins><br><br><br>官方推荐使用<code>deleteOne()</code>和<code>deleteMany()</code>方法：<br>    </p>
<p>如删除集合下全部文档：</p>
<pre><code>删除全部文档：
db.test.deleteMany(&#123;&#125;)

删除 status 等于 B 的一个文档：
db.test.deleteOne( &#123; status: &quot;B&quot; &#125; ) 
</code></pre>
</p><br>

<h3>MongoDB 查询文档<small>相关命令与语法</small></h3>
<p>
MongoDB查询文档并以<mark>非结构化形式</mark>显示的方法为find()命令。<br>
而<code>pretty()</code>方法是以易读的形式显示。<br>
<code>db.test.find().pretty();</code><br>

<p>
各种RDBMS（关系型数据库管理系统）中where条件的表达方式：

<pre><code>等于：&#123;key:value&#125;
小于：&#123;key: &#123;$lt:value&#125;&#125;
大于：&#123;key: &#123;$gt:value&#125;&#125;
小于或等于：&#123;key: &#123;$lte:value&#125;&#125;
大于或等于：&#123;key: &#123;$gte:value&#125;&#125;
不等于：&#123;key: &#123;$ne:value&#125;&#125;
</code></pre>
</p>

<h4>MongoDB AND 和条件</h4>
<p>
MongoDB中的查询可以传入多个值，其中用<code>，</code>逗号隔开就行。
<br>

<pre><code>&gt; db.test.find(&#123;&quot;name&quot;:&quot;vincent&quot;, &quot;age&quot;:&quot;18&quot;&#125;).pretty(); 
</code></pre>
</p>

<h4>MongoDB OR 或条件</h4>
<p>
MongoDB中的查询除了可以传入和条件，也可以传入OR条件
<br>

<pre><code>&gt; db.test.find(&#123;$or: [&#123;key1: value1&#125;, &#123;key2:value2&#125;]&#125;).pretty(); 
</code></pre>
</p>
</p>
</div>
</div>
</div>
</div>
</div>

<div class="bottom-p">
<!-- 集合部分 -->
<div class="panel-group" id="accordion">
<div class="panel panel-default">

<div id="limit" class="panel-collapse collapse in">
<div class="panel-body">
<h3>MongoDB $type 操作符<small>相关命令与语法</small></h3>
<p>
常见数据类型：<br>

<div class="table-container"><table class="table-bordered table-hover table-condensed">
<tr>
<td>Integer</td>
<td>数值类型，在计算机中可以是32也可以是64位</td>
</tr>
<tr>
<td>String</td>
<td>字符串类型，UTF-8编码</td>
</tr>
<tr>
<td>Double</td>
<td>浮点类型</td>
</tr>
<tr>
<td>Boolean</td>
<td>布尔类型，true或者false</td>
</tr>
<tr>
<td>Arrays</td>
<td>数组类型，可以存储多个相同类型的值</td>
</tr>
<tr>
<td>NULL</td>
<td>空值类型</td>
</tr>
<tr>
<td>Timestamp</td>
<td>时间戳</td>
</tr>
<tr>
<td>Code</td>
<td>存储JS代码于文档中</td>
</tr>
<tr>
<td>Regular expression</td>
<td>正则表达式</td>
</tr>
<tr>
<td>Binary data</td>
<td>存储二进制形式的数据</td>
</tr>
</table></div><br>
<strong>实例</strong><br>
假设已经插入数据名为Vincent的学生信息，其中name属性是以字符串形式存储的。<br>

<pre><code>db.test.insert(&#123;
name: &quot;Vincent&quot;
&#125;);
</code></pre>
<br>
现在想要访问name属性为string类型的数据，可以这么查询：<br>

<pre><code>db.test.find(&#123;&quot;title&quot;: &#123;$type : 2&#125;&#125;);
db.test.find(&#123;&quot;title&quot; : &#123;$type : &#39;string&#39;&#125;&#125;);
</code></pre>
</p>

<h3>MongoDB Limit() 分页<small>相关命令与语法</small></h3>        
<p>
与RDB一样，MongoDB中查询也可以<mark>指定读取的数据记录数</mark>，
往该方法中写入一个参数，即<strong>读取的数量</strong>。

<pre><code>db.test.find().limit(num);
</code></pre>
<p>其中，如果不指定数量，那么将会显示所有符合条件的数据记录。<br></p>
<br>

<p><strong>实例</strong><br></p>
<p>
假设已经存在三条数据，name属性分别为jack，vincent，rebecca。现在想要查询前两条：

<pre><code>db.test.find(&#123;&quot;name&quot; : 1, _id : 0&#125;).limit(2);
&#123;&quot;name&quot; : jack&#125; 
&#123;&quot;name&quot;: vincent&#125;
</code></pre>
</p>

</p>
</div>
</div>
</div>
</div>
</div>

<div class="bottom-p">
<!-- 集合部分 -->
<div class="panel-group" id="accordion">
<div class="panel panel-default">

<div id="sort" class="panel-collapse collapse in">
<div class="panel-body">
<h3>MongoDB sort()<small>相关命令与语法</small></h3>
<p>
在MySQL中，排序是通过<code>ORDER BY</code>来实现的，而在 MongoDB 中，排序是通过<code>sort()</code>
方法实现，并指定特定键的排序方式——升序或者降序（1或者-1）：

<pre><code>db.test.find().sort(&#123;key_name: 1&#125;);
</code></pre>
<p>上述代码将会以key_name升序的结果排序展示。</p>
<p>skip(), limilt(), sort()三个放在一起执行的时候，执行的顺序是先 sort(), 然后是 skip()，最后是显示的 limit()。</p>
</p>
</div>
</div>
</div>
</div>
</div>

<div class="bottom-p">
<!-- 索引与聚合 -->
<div class="panel-group" id="accordion">
<div class="panel panel-default">

<div id="index" class="panel-collapse collapse in">
<div class="panel-body">
<h3>MongoDB 索引<small>相关命令与语法</small></h3>
<p>
在处理非常大的数据时，可以想象，如果没有<mark>索引</mark>这样特殊的数据结构，查询时间将会花费几倍甚至几十倍的时间，
对网站的维护性能来说无疑是致命的。

<h4>createIndex() 创建索引</h4>
<p>
在MongoDB中使用该方法创建索引，同时可以传入参数，具体语法如下：<br><br>

<pre><code>&gt;db.test.createIndex(keys_name, options)
</code></pre>
<p>Key_name 值为你要创建的索引字段，<strong>可以以逗号隔开作为复合索引</strong>，<mark>1 为指定按升序创建索引，如果你想按降序来创建索引指定为 -1</mark>  即可。<br><br><br>options 是可选的额外参数：<br><br></p>
<div class="table-container"><table class="table-striped table-bordered table">
<tr>
<td>参数</td>
<td>类型</td>
<td>介绍</td>
</tr>
<tr>
<td>background</td>
<td>boolean</td>
<td>指定以“后台”方式建立索引，因为建立索引将会阻塞其他数据库操作，为了不影响多个操作的同时进行，指定为后台方式创建。</td>
</tr>
<tr>
<td>unique</td>
<td>boolean</td>
<td>“唯一”索引，默认值为false，开启则置为true</td>
</tr>
<tr>
<td>name</td>
<td>string</td>
<td>为索引指定一个名称，通过连接索引的字段名和排序顺序生成一个索引名称。</td>
</tr>
</table></div>
</p>
<strong>实例</strong><br><br>
<p>
创建一个以name和age属性并且升序的索引：<br>

<pre><code>db.test.createIndex(&#123;&quot;name&quot; : 1, &quot;age&quot; : 1&#125;);
</code></pre>
</p>

<p>常见关于索引的操作：<br>1、查看集合索引<br>db.col.getIndexes()</p>
<p>2、查看集合索引大小<br>db.col.totalIndexSize()</p>
<p>3、删除集合所有索引<br>db.col.dropIndexes()</p>
<p>4、删除集合指定索引<br>db.col.dropIndex(“索引名称”)</p>
<h3>MongoDB 聚合<small>相关命令与语法</small></h3>
<p>
<h4>aggregate() 方法</h4>
<p>
聚合一般用于统计数据，例如求最大值、平均数求和等等；<br>

<pre><code>&gt; db.test.aggregate(operation);
</code></pre>
<p>
假设集合中存在name为vincent、jack和rebecca的数据记录。<br>
并且所学科目名subject相应为：<br>
vincent所属分别为：《计算机1》、《计算机2》、《计算机3》，<br>
jack所属分别为：《文学1》、《文学2》，<br>
rebecca所属分别为：《经济史1》、《经济史2》、《经济史3》、《经济史4》<br>
</p>

<p>
现在需要得到每个人所阅读的科目总数，可以如下：

<pre><code>&gt; db.test.aggregate([&#123;$group : &#123;_id : &quot;$name&quot;, subject : &#123;$sum : 1&#125;&#125;&#125;])

&quot;result&quot; : [
 &quot;_id&quot; : &quot;runoob.com&quot;,
 &quot;num_tutorial&quot; : 2
&#125;,
&#123;
 &quot;_id&quot; : &quot;Neo4j&quot;,
 &quot;num_tutorial&quot; : 1
&#125;
],
&quot;ok&quot; : 1
&#125;
</code></pre>
<p>相当于SQL语句：</p>
<pre><code>select name, count(*) from mycol group by name
</code></pre>
</p>
<br><br>
常见的聚合表达式：<br>
<div class="table-container"><table class="table-bordered table-hover">
<tr>
<td>$sum</td>
<td>计算数据记录条数的总和</td>
</tr>
<tr>
<td>$avg</td>
<td>计算平均值</td>
</tr>
<tr>
<td>$min</td>
<td>计算对应组的最小值</td>
</tr>
<tr>
<td>$max</td>
<td>计算对应组的最大值</td>
</tr>
</table></div>
</p>
</p>
</div>
</div>
</div>
</div>
</div>
</div>
</div></div></article><article class="postlist-item post"><header class="post-header"><h1 class="post-title"><a class="post-title__link" href="/blog/2021/03/15/HTML/">HTML学习笔记</a></h1><div class="post-meta"><span class="post-meta-item post-meta-item--createtime"><span class="post-meta-item__icon"><i class="far fa-calendar-plus"></i></span><span class="post-meta-item__info">发表于</span><span class="post-meta-item__value">2021-03-15</span></span><span class="post-meta-item post-meta-item--updatetime"><span class="post-meta-item__icon"><i class="far fa-calendar-check"></i></span><span class="post-meta-item__info">更新于</span><span class="post-meta-item__value">2021-05-06</span></span><span class="post-meta-item post-meta-item--wordcount"><span class="post-meta-item__icon"><i class="far fa-file-word"></i></span><span class="post-meta-item__info">字数统计</span><span class="post-meta-item__value">2.9k</span></span><span class="post-meta-item post-meta-item--readtime"><span class="post-meta-item__icon"><i class="far fa-clock"></i></span><span class="post-meta-item__info">阅读时长</span><span class="post-meta-item__value">18分</span></span></div></header><div class="post-body"><div class="post-excerpt"><div id="html">
<h2>HTML<small>HyperText Markup Language</small></h2>
<blockquote>         
<p>
<i class="fa fa-quote-left"></i> 
&nbsp;&nbsp;&nbsp;&nbsp; 
前端开发是创建Web页面或app等前端界面呈现给用户的过程，
通过HTML，CSS及JavaScript以及衍生出来的各种技术、框架、解决方案，来实现互联网产品的用户界面交互。
它从网页制作演变而来，名称上有很明显的时代特征。在互联网的演化进程中，网页制作是Web1.0时代的产物，
早期网站主要内容都是静态，以图片和文字为主，用户使用网站的行为也以浏览为主。随着互联网技术的发展和HTML5、CSS3的应用，
现代网页更加美观，交互效果显著，功能更加强大。 移动互联网带来了大量高性能的移动终端设备以及快速的无线网络，HTML5，node.jS的广泛应用，各类框架类库层出不穷。 
<i class="fa fa-quote-right"></i>
<br>
<p>
<small style="position: relative; left: 500px;">——百度百科</small>
</p>
</p>
</blockquote>

<div class="reference clearfix"> 
<div class="title_content" style="margin-bottom:5px">
<div  class="text_content">笔记前言</div>
<div class="clear"></div>
</div>
</div>
<br>
<!-- 笔记重要概念 -->
<p class="im_concept">
<em>一：HTML</em>
<br>
&nbsp;&nbsp;&nbsp;&nbsp;首先我们需要知道什么是HTML，所谓HTML，就是Hyper Text Markup Language（超文本标记语言），
虽然它是一种语言，但是千万不要认为HTML是编程语言，它不过是一种标记语言罢了。而超文本，则指的是“超越”文本，比如图
片、超链接、音乐甚至程序；其中包括“头部”与“主体”部分。分别提供了网页信息与具体内容。
</p >
<p class="im_concept">
<em>二：CSS</em>
<br>
&nbsp;&nbsp;&nbsp;&nbsp;
CSS的全称是：Cascading Style Sheets（层叠样式表），就如同英文表达的那样，它的工作主要是静态地修饰网页，做出你想要的freestyle；
它有着这些特点：<br>
1.丰富的样式定义<br>
2.易于使用与修改<br>
3.多页面应用<br>
</p>
<p class="im_concept">
<em>三：JavaScript</em><br>
&nbsp;&nbsp;&nbsp;&nbsp;
与HTML、CSS不同的是，JavaScript（后文将直呼JS）才是一门真正的编程语言，也是一门直译式语言。用于动态地对网页中的DOM对象进行修改（增删改查）。
它的主要特点：<br>
1.是一种解释性语言，不需要像C语言那种语言，需要预编译。<br>
2.可以直接嵌入至HTML页面中，也可以将结构与功能进行分离（外部JS） <br>
3.跨平台特性，因为由浏览器支持，所以一处运行，多处使用。<br>
</p>    
<br>

<!-- 笔记正文 -->
<div class="reference clearfix"> 
<div class="title_content" style="margin-bottom:5px">
<div  class="text_content">笔记正文</div>
<div class="clear"></div>
</div>
</div>
<br>

<div class="bottom-p">
<!-- HTML基础 -->
<div class="panel-group" id="accordion">
<div class="panel panel-default">
<div class="panel-heading">
<h4 class="panel-title">
<a data-toggle="collapse" data-parent="#accordion" href="#html_base" class="collapse_tabs">
HTML 排版基础
<i class="glyphicon glyphicon-chevron-up" style="float: right;font-size: 13px;"></i>
</a>
</h4>
</div>
<div id="html_base" class="panel-collapse collapse in">
<div class="panel-body">
<h2>HTML <small>文档结构</small></h2>
<p>
在标准的html网页中，我们可以看到这些<mark>关键字</mark>（标签）<br><br>
<code>
&lt;!DOCTYPE&gt;        //代表文档类型<br>
&lt;html&gt;            //html文档内容<br>
&lt;head&gt;            //网站的头部<br>
&lt;body&gt;            //网站的主体部分<br>
</code>
</p>
<br>

<h2>HTML 标题<small>h1 - h6</small></h2>
<p>
html中的标题通过标签<em>&lt;h1&gt;&lt;/h1&gt;</em>到<em>&lt;h6&gt;&lt;/h6&gt;</em>，字体大小
逐渐变小而实现的。h6标题是最小的，继续添加h7将无效。
</p>
<p>
<code>
&lt;h1&gt; h1标题  &lt;/h1&gt;<br>
&lt;h2&gt; h2标题  &lt;/h2&gt;<br>
&lt;h3&gt; h3标题  &lt;/h3&gt;<br>
...<br>
&lt;h6&gt; h6标题  &lt;/h6&gt;<br>
</code>
</p>
<br>

<h2>HTML 段落<small>p</small></h2>
<p>
html中的段落通过标签<em>&lt;p&gt;&lt;/p&gt;</em>实现的。值得注意的是，在<code>p</code>的尾标签结束后，将会自动换行。
这一般也用于段落与段落之间的美化与排版。
</p>

<br>

<h2>HTML 超链接<small>a</small></h2>
<p>
有时我们需要网页与网页之间的跳转，可以用到其中一个方法：通过标签<em>&lt;a&gt;&lt;/a&gt;</em>实现。<br>
可以增加属性<strong>href</strong>指定链接的地址
<p>
<code>
&lt;a href="http://www.baidu.com"&gt; 点我进入百度  &lt;/a&gt;<br>
</code>

</p><br>

</p>

<h2>HTML 图像<small>img</small></h2>
<p>
有时我们需要在网页中增加图片美化文档，通过标签<em>&lt;img/&gt;</em>实现。<br>
可以增加属性<code>src</code>指定图像的来源，可以使用绝对路径或者相对路径：<br>

<blockquote>
<p>
相对路径：<br>
&nbsp;&nbsp;&nbsp;&nbsp;
<mark>相对路径是指目标相对于当前文件的路径</mark>，内部网站设计中多采用这样的结构，以便于寻找资源。
其常用方法如下：<br>
./ : 代表文件所在的目录，通常可以省略不写。<br>
../ : 代表文件所在的父级目录。<br>
/ :代表文件所在的根目录<br>
<br>
<mark>绝对路径是指完整的网址</mark>，比如超链接中的“https://www.baidu.com”这个网址，多用于外部资源url的
引入。开发中应避免在内部网站中使用绝对路径，除了不易维护，也会出现因改变目录地址而无法找到的错误。
</p>
</blockquote>

<p>
<code>
&lt;a href="http://www.baidu.com"&gt; 点我进入百度  &lt;/a&gt;<br>
</code>
</p><br>    
</p>

<h2>字符实体</h2>
<p>
&nbsp;&nbsp;&nbsp;&nbsp;
当你在编译器或者文本中敲写html代码时，也许你会发现这样一个有趣的问题：敲下<kbd><kbd>enter</kbd></kbd>键换到下一行继续写后，
网页中的内容，除非超出容器外，否则不会换行；当你想要打出"&lt;" 或者"&gt;"时，也许会影响到整个HTML文档的格式等等情况。<br>
&nbsp;&nbsp;&nbsp;&nbsp;
这时你需要到一些常见的<code>字符实体或者转义字符</code>，或者相关的标签：<br><br>
<div class="table-container"><table class="table table-bordered table-hover">
<tr>
<td>字符</td>
<td>具体含义</td>
</tr>

<tr>
<td>br单标签</td>
<td>换行</td>
</tr>

<tr>
<td>&+nbsp;(去掉中间的+号)</td>
<td>空格</td>
</tr>

<tr>
<td>&+lt;</td>
<td>小于</td>
</tr>

<tr>
<td>&+gt;</td>
<td>大于</td>
</tr>
</table></div>    
</p>
<br>

<br>

</div>
</div>
</div>
</div>
</div>

<div class="bottom-p">
<!-- HTML表格 -->
<div class="panel-group" id="accordion">
<div class="panel panel-default">
<div class="panel-heading">
<h4 class="panel-title">
<a data-toggle="collapse" data-parent="#accordion" href="#html_table" class="collapse_tabs">
HTML 表格
<i class="glyphicon glyphicon-chevron-up" style="float: right;font-size: 13px;"></i>
</a>
</h4>
</div>
<div id="html_table" class="panel-collapse collapse in">
<div class="panel-body">
<h2>HTML <small>表格结构</small></h2>
<p>
通常我们看到的HTML中的表格与excel中的表格，结构上没有太大的区别：<br>
表格由<code>table</code>标签来定义。而每个表格中由行与列构成，即
<code>tr</code>与<code>td</code>构成，<mark>且td标签需要内嵌于tr标签中，即若干个列属于同一行</mark>
每个td：table data（表格数据）中，可以放入图片、段落、表单甚至表格。    
<br><br>
注意：<ins>我们在td中写数据，请勿写在tr中td外！</ins>
<br><br>
</p>
<strong>实例</strong>
<br>
<p>
创建一个一行两列的默认表格：<br>
<code>
&lt;table&gt;<br>
&lt;tr&gt;<br>
&lt;td&gt;第一行第一列 &lt;/td&gt;<br>
&lt;td&gt;第一行第二列 &lt;/td&gt;<br>
&lt;/tr&gt; <br>

<p>&lt;/table&gt;<br></code></p>
</p><br>

<h2>HTML <small>表格重要属性与其他标签</small></h2>
<p>
<strong>边框</strong><br>
<p>
由于默认的表格是不带有边框的，可能在排版上不那么正规。
因此，引入<code>border</code>属性，其值从<code>1</code>开始递增。<br>
&nbsp;&nbsp;&nbsp;&nbsp;
带有边框的表格：
<br>
<code>
&lt;table border = "1" &gt;<br>
&lt;tr&gt;<br>
&lt;td&gt;第一行第一列 &lt;/td&gt;<br>
&lt;td&gt;第一行第二列 &lt;/td&gt;<br>
&lt;/tr&gt; <br>
&lt;/table&gt;
</code>
</p>
<br>
</p>
<p>
<strong>表格的表头标签</strong><small>th</small><br>
<p>
通常我们需要提示<code>表头</code>来提示用户，该表格的内容或者信息概要，
此时可能会需要表头，类似网站的<code>head</code>头部信息，大多数浏览器会显示为粗体并且居中的文本。
</p>
<br>

<p><strong>表格的标题标签</strong>caption<br></p>
<p>
表格的标题会被显示在，表格的正上方，且不会被边框环绕。<br>
<code>
&lt;caption&gt;标题&lt;/caption&gt;<br>
&lt;table border = "1" &gt;<br>
&lt;tr&gt;<br>
&lt;td&gt;第一行第一列 &lt;/td&gt;<br>
&lt;td&gt;第一行第二列 &lt;/td&gt;<br>
&lt;/tr&gt; <br>
&lt;/table&gt;
</code>
</p>
</p>
</div>
</div>
</div>
</div>
</div>

<div class="bottom-p">
<!-- HTML表格 -->
<div class="panel-group" id="accordion">
<div class="panel panel-default">
<div class="panel-heading">
<h4 class="panel-title">
<a data-toggle="collapse" data-parent="#accordion" href="#html_list" class="collapse_tabs">
HTML 列表
<i class="glyphicon glyphicon-chevron-up" style="float: right;font-size: 13px;"></i>
</a>
</h4>
</div>
<div id="html_list" class="panel-collapse collapse in">
<div class="panel-body">
<h2>无序列表<small>ul>li</small></h2>
<p>
对于列表，我们可以联想到家里的购物清单。也许是有序，也许是无序。因此我们引入，列表的概念：<br>
<br><br>
无序列表<code>unordered list</code>的语法如下：<br>
<p>
<code>
&lt;ul&gt;    <br>
&lt;li&gt; 数据 &lt;/li&gt;<br>
&lt;/ul&gt;<br>
</code>
</p>
<mark>默认情况下，此列表的数据前会有典型的小黑圆圈标记。</mark>
<br>
</p>

<h2>有序列表<small>ol>li</small></h2>
<p>

<p>有序列表<code>ordered list</code>的语法如下：<br></p>
<p>
<code>
&lt;ol&gt;    <br>
&lt;li&gt; 数据 &lt;/li&gt;<br>
&lt;/ol&gt;<br>
</code>
</p>
<mark>默认情况下，此列表的数据前会有数字标记。</mark><br>
<b>我们可以通过更改属性<code>type</code>来更改有序列表数据展示的样式：<br></b>
<br>
<code>
可以是"A" "I" "i" "1"分别代表不同的排列顺序！
</code>
<br>
</p>
</div>
</div>
</div>
</div>
</div>

<div class="bottom-p">
<!-- HTML表格 -->
<div class="panel-group" id="accordion">
<div class="panel panel-default">
<div class="panel-heading">
<h4 class="panel-title">
<a data-toggle="collapse" data-parent="#accordion" href="#html_area" class="collapse_tabs">
HTML 区块
<i class="glyphicon glyphicon-chevron-up" style="float: right;font-size: 13px;"></i>
</a>
</h4>
</div>
<div id="html_area" class="panel-collapse collapse in">
<div class="panel-body">
<h2>块级元素<small>div</small></h2>
<p>
我们将div元素认定为块级元素，是因为它具有以下几个特点：<br>
1.总是在新行开始内容<br>
2.高度、行高、内外边距均可调整（具体内容将会在CSS中讲到）<br>
3.宽度默认为它容器的100%<br>
</p>
<p>
<h4>语法如下：</h4>
<code>&lt;div&gt;
<br>
其他元素或者内容
<br>
&lt;/div&gt;
</code>
</p>

<h2>行内元素<small>span</small></h2>
<p>
我们将span元素认定为内联元素（行内元素），是因为它具有以下几个特点：<br>
1.不会在新行开始<br>
2.高度等属性不可改变<br>
3.宽度一般就为容器中的文字或者图片的高度<br>
4.只能容纳文本等行内元素<br>
</p>
<p>
<h4>语法如下：</h4>
<code>&lt;span&gt;
<br>
容器中的内容
<br>
&lt;/span&gt;
</code>
</p>
</div>
</div>
</div>
</div>
</div>

<div class="bottom-p">
<!-- HTML表单 -->
<div class="panel-group" id="accordion">
<div class="panel panel-default">
<div class="panel-heading">
<h4 class="panel-title">
<a data-toggle="collapse" data-parent="#accordion" href="#html_form" class="collapse_tabs">
HTML 表单
<i class="glyphicon glyphicon-chevron-up" style="float: right;font-size: 13px;"></i>
</a>
</h4>
</div>
<div id="html_form" class="panel-collapse collapse in">
<div class="panel-body">
<h2>表单输入<small>form>input</small></h2>
<h3>表单form</h3>
<p>
去应聘的时候，工作人员往往会给你一张表单让你填写你的个人信息，包括姓名，年龄，兴趣爱好，个人介绍等。
</p>
<p>
表单的语法如下：<br>
<code>
&lt;form&gt;<br>
填写input各类型的元素<br>
&lt;/form&gt;<br>    
</code>
</p>

<p>
<code>
表单有如下重要属性：<br>
一：处理程序属性action：URL（表单提交的地址）<br>
二：表单名称name：不同的表单尽量使用不同的名字，避免混淆<br>
三：提交方法，GET与POST：<br>
<p>
首先get是默认的提交方式，它会将表单中的输入元素等相关参数，附在网页的URL后；<br>
因此我们可以看出，<mark>对于比较敏感的数据，它是不太安全的。</mark><br>
但是，它是主动推送数据给服务器的，因此比起post，<mark>它更快速。</mark>
</p>
四：编码属性：（multipart/form-data）MIME多用途网络邮件<br>
顶级媒体类型/子类型（后缀名）
text：文本<br>
application：应用<br>
multipart：多部分媒体<br>
audio：音频文件<br>
image：图片<br>
message：消息<br>
</code>
</p>        


<h3>输入input</h3>
<p>
输入元素的语法如下：<br>
<code>
&lt;input type = "input_type"&gt;<br>    
</code>
</p>

<p>
输入元素被包含在<code>form</code>中，常见的元素类型<code>type</code>被列举如下：<br>
<h4>文本域<small>text</small></h4>
<p>
语法如下：<br>
<code>
&lt;form&gt;<br>
&lt;input type = "text"&gt;<br>    
&lt;/form&gt;<br>    
</code>
</p>

<p>
我们通常在文本域中填写可见的文字或者数字，<mark>其默认长度为20字符</mark>，
</p>

<h4>密码域<small>password</small></h4>
<p>
密码字符不会明码显示（废话），将会以黑色星点或者圆点代替。语法如下：<br>
<code>
&lt;form&gt;<br>
&lt;input type = "password"&gt;<br>    
&lt;/form&gt;<br>    
</code>
</p>

<h4>单选框<small>radio</small></h4>
<p>
如果限选一个选项，我们需要用单选框，<mark>注意，请给他们赋予
相同的<code>name</code>属性；请
将给用户看的选项与你input输入元素的value属性值相等。</mark>比如：<br>
<code>吧、
&lt;form&gt;<br>
&lt;input type = "radio" name = "sex" value = "男"&gt;男<br>    
&lt;input type = "radio" name = "sex" value = "女"&gt;女<br>    
&lt;/form&gt;<br>    
</code>
</p>

<h4>多选框<small>checkbox</small></h4>
<p>
从若干个选项中选取一个或多个，我们需要用多选框，仍旧建议使用相同的name，并且value值与显示出来的值相同：<br>
<code>
&lt;form&gt;<br>
&lt;input type = "checkbox" name = "vehicle" value = "轿车"&gt;轿车<br>    
&lt;input type = "checkbox" name = "vehicle" value = "自行车"&gt;自行车<br>    
&lt;/form&gt;<br>    
</code>
</p>

<h4>提交按钮<small>submit</small></h4>
<p>
填写完所有服务器需要的信息后，我们可以添加提交按钮，表示信息准备提交至服务器:<br>
<code>
&lt;form&gt;<br>
&lt;input type = "submit" value = "提交"&gt;提交<br>    
&lt;/form&gt;<br>    
</code>
</p>

<p>
<strong>请注意</strong>
提交按钮<code>submit</code>与普通按钮<code>button</code>的区别：<br>
<p>
1.一般submit用于表单提交；而button仅仅是一个按钮，只要绑定不同的事件，就能实现不同的功能。
<br>2.submit与button修改按钮上的文字的方式也不同，对于submit，有自己的属性<code>value</code>，
我们用JS捕获到该DOM对象后，通过修改这个属性值来修改文字；button通过直接修改innerHTML值来修改其按钮上的文本值。
</p>
</p>

</p>
</div>
</div>
</div>
</div>
</div>

<div class="bottom-p">
<!-- HTML颜色 -->
<div class="panel-group" id="accordion">
<div class="panel panel-default">
<div class="panel-heading">
<h4 class="panel-title">
<a data-toggle="collapse" data-parent="#accordion" href="#html_color" class="collapse_tabs">
HTML 颜色
<i class="glyphicon glyphicon-chevron-up" style="float: right;font-size: 13px;"></i>
</a>
</h4>
</div>
<div id="html_color" class="panel-collapse collapse in">
<div class="panel-body">
<h2>HTML中的颜色<small>十六进制或者RGB（Red，Green，Blue）</small></h2>
<p>
HTML中的颜色可以由十六进制来定义，这个符号由相应RGB的值来确定；<mark>每个值的区间是[0(#00),255(#FF)]</mark>
下面列举常用颜色的RGB值：<br>
<code>
#000000 / rgb(0,0,0)：   黑色<br>
#FF0000 / rgb(255,0,0)： 红色<br>
#00FF00 / rgb(0,255,0)： 绿色<br>
#0000FF / rgb(0,0,255)： 蓝色<br>
#FFFFFF    / rgb(255,255,255): 白色<br>                                
</code>
</p>
</div>
</div>
</div>
</div>
</div>
</div></div></div></article><article class="postlist-item post"><header class="post-header"><h1 class="post-title"><a class="post-title__link" href="/blog/2021/03/15/%E7%BF%BB%E8%AF%91%E4%BD%9C%E5%93%81-%E5%9B%9E%E7%AD%94%E6%88%91%E4%B8%8D%E7%9F%A5%E9%81%93%E7%9A%84%E5%85%AB%E7%A7%8D%E6%96%B9%E5%BC%8F/">翻译作品-替换“我不知道”的八种更好的回答</a></h1><div class="post-meta"><span class="post-meta-item post-meta-item--createtime"><span class="post-meta-item__icon"><i class="far fa-calendar-plus"></i></span><span class="post-meta-item__info">发表于</span><span class="post-meta-item__value">2021-03-15</span></span><span class="post-meta-item post-meta-item--updatetime"><span class="post-meta-item__icon"><i class="far fa-calendar-check"></i></span><span class="post-meta-item__info">更新于</span><span class="post-meta-item__value">2021-03-15</span></span><span class="post-meta-item post-meta-item--wordcount"><span class="post-meta-item__icon"><i class="far fa-file-word"></i></span><span class="post-meta-item__info">字数统计</span><span class="post-meta-item__value">379</span></span><span class="post-meta-item post-meta-item--readtime"><span class="post-meta-item__icon"><i class="far fa-clock"></i></span><span class="post-meta-item__info">阅读时长</span><span class="post-meta-item__value">3分</span></span></div></header><div class="post-body"><div class="post-excerpt"><!-- 页面必备样式 -->
<style>
.back-top{position: fixed;top: 500px;height: 0px;float: right;right: 50px;}
.back-top>a{font-weight:bold;width: 50px;height: 50px;text-align: center;text-decoration: none;font-size: 5px;line-height: 50px;border-radius: 50px;background-color:rgba(255,255,255,0.5);}
</style>
<div class="back-top">
    <a href="#top"> >返回顶部< </a>
</div>
<!-- toc -->
<!-- 必备样式结束 -->


        <h2 id="Improve-your-Vocabulary-8-better-ways-to-say-“I-don’t-know”"   >
          <a href="#Improve-your-Vocabulary-8-better-ways-to-say-“I-don’t-know”" class="heading-link"><i class="fas fa-link"></i></a><a href="#Improve-your-Vocabulary-8-better-ways-to-say-“I-don’t-know”" class="headerlink" title="Improve your Vocabulary: 8 better ways to say “I don’t know”"></a>Improve your Vocabulary: 8 better ways to say “I don’t know”</h2>
      
        <h2 id="提高你的词汇量-8种更好的表达“我不知道”的方法"   >
          <a href="#提高你的词汇量-8种更好的表达“我不知道”的方法" class="heading-link"><i class="fas fa-link"></i></a><a href="#提高你的词汇量-8种更好的表达“我不知道”的方法" class="headerlink" title="提高你的词汇量:8种更好的表达“我不知道”的方法"></a>提高你的词汇量:8种更好的表达“我不知道”的方法</h2>
      <p>When someone asks you something and you don’t know the answer, it’s okay to admit that you don’t know. But you can respond with more than “I don’t know” – you can use richer vocabulary, you can be more polite, or you can offer to help. In this lesson, I’ll give you 8 ways you can respond in English when you don’t know the answer. You’ll learn expressions such as “I’d be happy to…”, “Before I can answer…”, “Honestly, I have no idea”, “If I knew, I would…”, and more. Start having better English conversations by learning and using these expressions. Take the quiz to make sure you understood the lesson! <span class="exturl"><a class="exturl__link"   target="_blank" rel="noopener" href="https://www.engvid.com/" >https://www.engvid.com/</a><span class="exturl__icon"><i class="fas fa-external-link-alt"></i></span></span></p>
<p>当别人问你什么而你不知道答案的时候，承认自己不知道没什么大不了的。但是你可以用比“我不知道”更多的回答——你可以使用更丰富的词汇，你可以更有礼貌，或者你可以主动提供帮助。这节课，我将告诉你不知道答案时8种用英语回答的方法。你会学到诸如“我很乐意……”，“在我能回答……之前”，“老实说，我不知道”，“如果我知道，我就会……”等等的表达。通过学习和使用这些表达来开始更好的英语对话。做个小测验，确保你理解了课文!<span class="exturl"><a class="exturl__link"   target="_blank" rel="noopener" href="https://www.engvid.com/" >https://www.engvid.com/</a><span class="exturl__icon"><i class="fas fa-external-link-alt"></i></span></span></p>
<div style="position: relative; padding: 30% 45%;">
<iframe style="position: absolute; width: 100%; height: 100%; left: 0; top: 0;" 
src="https://player.bilibili.com/player.html?aid=844619251&bvid=BV1B54y1a7xQ&cid=310374887&page=1"
 frameborder="no" scrolling="no"></iframe>
</div></div></div></article><article class="postlist-item post"><header class="post-header"><h1 class="post-title"><a class="post-title__link" href="/blog/2021/03/13/%E7%BF%BB%E8%AF%91%E4%BD%9C%E5%93%81-%E5%9C%A8%E6%B3%95%E5%9B%BD%E7%9A%84%E7%BE%8E%E5%9B%BD%E5%B0%8F%E4%BC%99%E5%84%BF/">翻译作品_在法国的美国小伙儿</a></h1><div class="post-meta"><span class="post-meta-item post-meta-item--createtime"><span class="post-meta-item__icon"><i class="far fa-calendar-plus"></i></span><span class="post-meta-item__info">发表于</span><span class="post-meta-item__value">2021-03-13</span></span><span class="post-meta-item post-meta-item--updatetime"><span class="post-meta-item__icon"><i class="far fa-calendar-check"></i></span><span class="post-meta-item__info">更新于</span><span class="post-meta-item__value">2021-03-15</span></span><span class="post-meta-item post-meta-item--wordcount"><span class="post-meta-item__icon"><i class="far fa-file-word"></i></span><span class="post-meta-item__info">字数统计</span><span class="post-meta-item__value">83</span></span><span class="post-meta-item post-meta-item--readtime"><span class="post-meta-item__icon"><i class="far fa-clock"></i></span><span class="post-meta-item__info">阅读时长</span><span class="post-meta-item__value">1分</span></span></div></header><div class="post-body"><div class="post-excerpt"><!-- 页面必备样式 -->
<style>
.back-top{position: fixed;top: 500px;height: 0px;float: right;right: 50px;}
.back-top>a{font-weight:bold;width: 50px;height: 50px;text-align: center;text-decoration: none;font-size: 5px;line-height: 50px;border-radius: 50px;background-color:rgba(255,255,255,0.5);}
</style>
<div class="back-top">
    <a href="#top"> >返回顶部< </a>
</div>
<!-- toc -->
<!-- 必备样式结束 -->


        <h2 id="该视频由YouTube博主讲述如何作为一名美国人-融入法国的生活"   >
          <a href="#该视频由YouTube博主讲述如何作为一名美国人-融入法国的生活" class="heading-link"><i class="fas fa-link"></i></a><a href="#该视频由YouTube博主讲述如何作为一名美国人-融入法国的生活" class="headerlink" title="该视频由YouTube博主讲述如何作为一名美国人 融入法国的生活"></a>该视频由YouTube博主讲述如何作为一名美国人 融入法国的生活</h2>
      
        <h2 id="第一弹："   >
          <a href="#第一弹：" class="heading-link"><i class="fas fa-link"></i></a><a href="#第一弹：" class="headerlink" title="第一弹："></a>第一弹：</h2>
      <div style="position: relative; padding: 30% 45%;">
<iframe style="position: absolute; width: 100%; height: 100%; left: 0; top: 0;" 
src="https://player.bilibili.com/player.html?aid=844141883&bvid=BV1P54y1Y741&cid=297738729&page=1"
 frameborder="no" scrolling="no"></iframe>
</div>


        <h2 id="第二弹："   >
          <a href="#第二弹：" class="heading-link"><i class="fas fa-link"></i></a><a href="#第二弹：" class="headerlink" title="第二弹："></a>第二弹：</h2>
      <div style="position: relative; padding: 30% 45%;">
<iframe style="position: absolute; width: 100%; height: 100%; left: 0; top: 0;" 
src="https://player.bilibili.com/player.html?aid=416684897&bvid=BV1yV411i7dC&cid=297931818&page=1"
 frameborder="no" scrolling="no"></iframe>
</div>



        <h2 id="第三弹："   >
          <a href="#第三弹：" class="heading-link"><i class="fas fa-link"></i></a><a href="#第三弹：" class="headerlink" title="第三弹："></a>第三弹：</h2>
      <div style="position: relative; padding: 30% 45%;">
<iframe style="position: absolute; width: 100%; height: 100%; left: 0; top: 0;" 
src="https://player.bilibili.com/player.html?aid=671645337&bvid=BV1cU4y1W7u1&cid=298333318&page=1"
 frameborder="no" scrolling="no"></iframe>
</div>
</div></div></article><article class="postlist-item post"><header class="post-header"><h1 class="post-title"><a class="post-title__link" href="/blog/2021/03/11/PYTHON%20%E4%BB%8E%E5%85%A5%E9%97%A8%E5%88%B0%E7%B2%BE%E9%80%9A-%E5%88%A9%E7%94%A8BeautifulSoup%20Get%20%E4%B8%8E%20Post%20%E8%AF%B7%E6%B1%82/">PYTHON 从入门到精通-Get 与 Post 请求</a></h1><div class="post-meta"><span class="post-meta-item post-meta-item--createtime"><span class="post-meta-item__icon"><i class="far fa-calendar-plus"></i></span><span class="post-meta-item__info">发表于</span><span class="post-meta-item__value">2021-03-11</span></span><span class="post-meta-item post-meta-item--updatetime"><span class="post-meta-item__icon"><i class="far fa-calendar-check"></i></span><span class="post-meta-item__info">更新于</span><span class="post-meta-item__value">2021-03-15</span></span><span class="post-meta-item post-meta-item--wordcount"><span class="post-meta-item__icon"><i class="far fa-file-word"></i></span><span class="post-meta-item__info">字数统计</span><span class="post-meta-item__value">405</span></span><span class="post-meta-item post-meta-item--readtime"><span class="post-meta-item__icon"><i class="far fa-clock"></i></span><span class="post-meta-item__info">阅读时长</span><span class="post-meta-item__value">3分</span></span></div></header><div class="post-body"><div class="post-excerpt"><!-- 页面必备样式 -->
<style>
.back-top{position: fixed;top: 500px;height: 0px;float: right;right: 50px;}
.back-top>a{font-weight:bold;width: 50px;height: 50px;text-align: center;text-decoration: none;font-size: 5px;line-height: 50px;border-radius: 50px;background-color:rgba(255,255,255,0.5);}
</style>
<div class="back-top">
    <a href="#top"> >返回顶部< </a>
</div>
<!-- toc -->
<!-- 必备样式结束 -->


        <h1 id="实现Get请求-模拟百度搜索"   >
          <a href="#实现Get请求-模拟百度搜索" class="heading-link"><i class="fas fa-link"></i></a><a href="#实现Get请求-模拟百度搜索" class="headerlink" title="实现Get请求 模拟百度搜索"></a>实现Get请求 模拟百度搜索</h1>
      <figure class="highlight py"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> requests</span><br><span class="line"><span class="keyword">import</span> webbrowser</span><br><span class="line"><span class="keyword">if</span> __name__ == <span class="string">&#x27;__main__&#x27;</span>:</span><br><span class="line"></span><br><span class="line">    <span class="comment"># 参数用字典的方式 输入相应的key-value值</span></span><br><span class="line">    param = &#123;<span class="string">&#x27;wd&#x27;</span>: <span class="built_in">input</span>(<span class="string">&#x27;请输入你要搜索的内容：&#x27;</span>)&#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment"># 模拟实现百度搜索 其中地址栏只需要wd参数 后面跟的是 搜索的内容</span></span><br><span class="line">    req = requests.get(<span class="string">&#x27;http://www.baidu.com/s&#x27;</span>, params=param)</span><br><span class="line">    <span class="comment"># 最后利用内嵌的浏览器 打开</span></span><br><span class="line">    webbrowser.<span class="built_in">open</span>(req.url)</span><br></pre></td></tr></table></div></figure>


        <h1 id="利用Post-请求登录某页面"   >
          <a href="#利用Post-请求登录某页面" class="heading-link"><i class="fas fa-link"></i></a><a href="#利用Post-请求登录某页面" class="headerlink" title="利用Post 请求登录某页面"></a>利用Post 请求登录某页面</h1>
      <figure class="highlight py"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"><span class="keyword">import</span> requests</span><br><span class="line"><span class="keyword">import</span> webbrowser</span><br><span class="line"><span class="keyword">if</span> __name__ == <span class="string">&#x27;__main__&#x27;</span>:</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    <span class="comment"># 给到服务器要的参数</span></span><br><span class="line">    data = &#123;<span class="string">&#x27;firstname&#x27;</span>: <span class="string">&#x27;vincent&#x27;</span>, <span class="string">&#x27;lastname&#x27;</span>: <span class="string">&#x27;he&#x27;</span>&#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment"># 去目标action页面(注意 不总是登录填写账号密码的页面） Network中的FormData查看需要什么参数</span></span><br><span class="line">    response = requests.post(<span class="string">&#x27;https://pythonscraping.com/pages/files/processing.php&#x27;</span>, data=data)</span><br><span class="line">    <span class="built_in">print</span>(response.text)</span><br><span class="line"></span><br></pre></td></tr></table></div></figure>


        <h1 id="利用Cookies来保持登录状态"   >
          <a href="#利用Cookies来保持登录状态" class="heading-link"><i class="fas fa-link"></i></a><a href="#利用Cookies来保持登录状态" class="headerlink" title="利用Cookies来保持登录状态"></a>利用Cookies来保持登录状态</h1>
      <figure class="highlight py"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"><span class="keyword">import</span> requests</span><br><span class="line"><span class="keyword">import</span> webbrowser</span><br><span class="line"><span class="keyword">if</span> __name__ == <span class="string">&#x27;__main__&#x27;</span>:</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    <span class="comment"># 给到服务器要的参数</span></span><br><span class="line">    data = &#123;<span class="string">&#x27;username&#x27;</span>: <span class="string">&#x27;vincent&#x27;</span>, <span class="string">&#x27;password&#x27;</span>: <span class="string">&#x27;password&#x27;</span>&#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment"># 去目标action页面(注意 不总是登录填写账号密码的页面） Network中的FormData查看需要什么参数</span></span><br><span class="line">    response = requests.post(<span class="string">&#x27;http://pythonscraping.com/pages/cookies/welcome.php&#x27;</span>, data=data)</span><br><span class="line"></span><br><span class="line">    <span class="comment"># 在登录成功后 拿到相应的cookies值 可以直接用get去到相应的其他页面（这个页面需要登录之后 才能查看的） 不用再次登录！！！</span></span><br><span class="line">    response = requests.get(<span class="string">&#x27;http://pythonscraping.com/pages/cookies/profile.php&#x27;</span>, cookies=response.cookies)</span><br><span class="line">    <span class="built_in">print</span>(response.text)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></div></figure></div></div></article><article class="postlist-item post"><header class="post-header"><h1 class="post-title"><a class="post-title__link" href="/blog/2021/03/11/PYTHON%20%E4%BB%8E%E5%85%A5%E9%97%A8%E5%88%B0%E7%B2%BE%E9%80%9A-%E5%88%A9%E7%94%A8BeautifulSoup%20%E5%A6%82%E4%BD%95%E7%88%AC%E5%8F%96%E7%BD%91%E7%AB%99%E6%95%B0%E6%8D%AE/">PYTHON 从入门到精通-利用BeautifulSoup 如何爬取网站数据</a></h1><div class="post-meta"><span class="post-meta-item post-meta-item--createtime"><span class="post-meta-item__icon"><i class="far fa-calendar-plus"></i></span><span class="post-meta-item__info">发表于</span><span class="post-meta-item__value">2021-03-11</span></span><span class="post-meta-item post-meta-item--updatetime"><span class="post-meta-item__icon"><i class="far fa-calendar-check"></i></span><span class="post-meta-item__info">更新于</span><span class="post-meta-item__value">2021-03-15</span></span><span class="post-meta-item post-meta-item--wordcount"><span class="post-meta-item__icon"><i class="far fa-file-word"></i></span><span class="post-meta-item__info">字数统计</span><span class="post-meta-item__value">362</span></span><span class="post-meta-item post-meta-item--readtime"><span class="post-meta-item__icon"><i class="far fa-clock"></i></span><span class="post-meta-item__info">阅读时长</span><span class="post-meta-item__value">3分</span></span></div></header><div class="post-body"><div class="post-excerpt"><!-- 页面必备样式 -->
<style>
.back-top{position: fixed;top: 500px;height: 0px;float: right;right: 50px;}
.back-top>a{font-weight:bold;width: 50px;height: 50px;text-align: center;text-decoration: none;font-size: 5px;line-height: 50px;border-radius: 50px;background-color:rgba(255,255,255,0.5);}
</style>
<div class="back-top">
    <a href="#top"> >返回顶部< </a>
</div>
<!-- toc -->
<!-- 必备样式结束 -->

<figure class="highlight py"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"><span class="comment">#-*- coding:UTF-8 -*-</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 模拟定义一个完整的HTML源码</span></span><br><span class="line"><span class="keyword">from</span> attr <span class="keyword">import</span> attrs</span><br><span class="line"></span><br><span class="line">html = <span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="string">&lt;html&gt;</span></span><br><span class="line"><span class="string">&lt;head&gt;</span></span><br><span class="line"><span class="string">    &lt;title&gt;test&lt;/title&gt;</span></span><br><span class="line"><span class="string">&lt;/head&gt;</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">&lt;body&gt;</span></span><br><span class="line"><span class="string">&lt;p class=&#x27;title&#x27; name=&#x27;title_name&#x27;&gt;&lt;b&gt;once upon a time&lt;/b&gt;&lt;/p&gt;</span></span><br><span class="line"><span class="string">&lt;p class=&#x27;title1&#x27; name=&#x27;title_name1&#x27;&gt;&lt;b&gt;once upon a time1&lt;/b&gt;&lt;/p&gt;</span></span><br><span class="line"><span class="string">&lt;p class=&#x27;title2&#x27; value=&#x27;123&#x27; name=&#x27;title_name2&#x27;&gt;&lt;b&gt;once upon a time2&lt;/b&gt;&lt;/p&gt;</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">&lt;/body&gt;</span></span><br><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">from</span> bs4 <span class="keyword">import</span> BeautifulSoup</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">soup = BeautifulSoup(html, <span class="string">&#x27;lxml&#x27;</span>) <span class="comment"># lxml是解析方式 html 是模拟的页面</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 标准缩进方式</span></span><br><span class="line"><span class="comment"># soup.prettify()</span></span><br><span class="line"></span><br><span class="line">title = soup.title</span><br><span class="line"><span class="built_in">print</span>(title.text)  <span class="comment"># 选择标签为title的text内容（String属性也可以） 只选择第一个</span></span><br><span class="line"><span class="built_in">print</span>()</span><br><span class="line"></span><br><span class="line">head = soup.find(<span class="string">&#x27;head&#x27;</span>)</span><br><span class="line"><span class="built_in">print</span>(head.title.text)  <span class="comment"># title是head的子节点，那么可以通过逗号 直接索引到title并打印该标签中的内容</span></span><br><span class="line"><span class="built_in">print</span>()</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">p_one = soup.find(<span class="string">&#x27;p&#x27;</span>)  <span class="comment"># find函数 一样的做法  p 标签内容 只选择第一个</span></span><br><span class="line"><span class="built_in">print</span>(p_one.text)</span><br><span class="line"><span class="built_in">print</span>()</span><br><span class="line"></span><br><span class="line">p_one_class = soup.find(class_=<span class="string">&#x27;title&#x27;</span>)  <span class="comment"># find函数 类名寻找器 只选择第一个</span></span><br><span class="line"><span class="built_in">print</span>(p_one_class)</span><br><span class="line"><span class="built_in">print</span>()</span><br><span class="line"></span><br><span class="line">p_s = soup.find_all(<span class="string">&#x27;p&#x27;</span>)  <span class="comment"># find_all 也是查找 不过返回的是列表！！！应用for循环迭代其中的元素</span></span><br><span class="line"><span class="keyword">for</span> i, item <span class="keyword">in</span> <span class="built_in">enumerate</span>(p_s):</span><br><span class="line">    <span class="built_in">print</span>(i, item.text)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></div></figure></div></div></article></section><nav class="paginator"><div class="paginator-inner"><a class="extend prev" rel="prev" href="/blog/page/3/"><i class="fas fa-angle-left"></i></a><a class="page-number" href="/blog/">1</a><span class="space">&hellip;</span><a class="page-number" href="/blog/page/3/">3</a><span class="page-number current">4</span><a class="page-number" href="/blog/page/5/">5</a><a class="extend next" rel="next" href="/blog/page/5/"><i class="fas fa-angle-right"></i></a></div></nav></div></div><div class="sidebar-wrap" id="sidebar-wrap"><aside class="sidebar" id="sidebar"><section class="sidebar-toc hide"></section><!-- ov = overview--><section class="sidebar-ov"><div class="sidebar-ov-author"><div class="sidebar-ov-author__avatar"><img class="sidebar-ov-author__avatar_img" src="/blog/images/avatar.jpg" alt="avatar"></div><p class="sidebar-ov-author__text">C'est la vie</p></div><div class="sidebar-ov-state"><a class="sidebar-ov-state-item sidebar-ov-state-item--posts" href="/blog/archives/"><div class="sidebar-ov-state-item__count">48</div><div class="sidebar-ov-state-item__name">归档</div></a><a class="sidebar-ov-state-item sidebar-ov-state-item--categories" href="/blog/categories/"><div class="sidebar-ov-state-item__count">19</div><div class="sidebar-ov-state-item__name">分类</div></a><a class="sidebar-ov-state-item sidebar-ov-state-item--tags" href="/blog/tags/"><div class="sidebar-ov-state-item__count">20</div><div class="sidebar-ov-state-item__name">标签</div></a></div><div class="sidebar-ov-cc"><a href="https://creativecommons.org/licenses/by-nc-sa/4.0/deed.en" target="_blank" rel="noopener" data-popover="知识共享许可协议" data-popover-pos="up"><img src="/blog/images/cc-by-nc-sa.svg"></a></div></section></aside></div><div class="clearfix"></div></div></main><footer class="footer" id="footer"><div class="footer-inner"><div><span>Copyright © 2021</span><span class="footer__icon"><i class="fas fa-heart"></i></span><span>何水浪</span></div><div><span>由 <a href="http://hexo.io/" title="Hexo" target="_blank" rel="noopener">Hexo</a> 强力驱动</span><span> v5.4.0</span><span class="footer__devider">|</span><span>主题 - <a href="https://github.com/liuyib/hexo-theme-stun/" title="Stun" target="_blank" rel="noopener">Stun</a></span><span> v2.6.2</span></div></div></footer><div class="loading-bar" id="loading-bar"><div class="loading-bar__progress"></div></div><div class="back2top" id="back2top"><span class="back2top__icon"><i class="fas fa-rocket"></i></span></div></div><div class="search-mask"></div><div class="search-popup"><span class="search-close"></span><div class="search-input"><input placeholder="搜索文章（支持多关键词，请用空格分隔）"></div><div class="search-results"></div></div><script src="https://cdn.jsdelivr.net/npm/jquery@v3.4.1/dist/jquery.min.js"></script><script src="https://cdn.jsdelivr.net/npm/velocity-animate@1.5.2/velocity.min.js"></script><script src="https://cdn.jsdelivr.net/npm/velocity-animate@1.5.2/velocity.ui.min.js"></script><script>function initSearch() {
  var isXML = true;
  var search_path = 'search.json';

  if (!search_path) {
    search_path = 'search.xml';
  } else if (/json$/i.test(search_path)) {
    isXML = false;
  }

  var path = '/blog/' + search_path;
  $.ajax({
    url: path,
    dataType: isXML ? 'xml' : 'json',
    async: true,
    success: function (res) {
      var datas = isXML ? $('entry', res).map(function () {
        // 将 XML 转为 JSON
        return {
          title: $('title', this).text(),
          content: $('content', this).text(),
          url: $('url', this).text()
        };
      }).get() : res;
      var $input = $('.search-input input');
      var $result = $('.search-results');
      // 搜索对象（标题、内容）的权重，影响显示顺序
      var WEIGHT = { title: 100, content: 1 };
      var searchPost = function () {
        var searchText = $input.val().toLowerCase().trim();
        // 根据空白字符分隔关键字
        var keywords = searchText.split(/[\s]+/);
        // 搜索结果
        var matchPosts = [];

        // 有多个关键字时，将原文字整个保存下来
        if (keywords.length > 1) {
          keywords.push(searchText);
        }
        // 防止未输入字符时搜索
        if (searchText.length > 0) {
          datas.forEach(function (data) {
            var isMatch  = false;
            // 没有标题的文章使用预设的 i18n 变量代替
            var title = (data.title && data.title.trim()) || '[ 文章无标题 ]';
            var titleLower = title && title.toLowerCase();
            // 删除 HTML 标签 和 所有空白字符
            var content = data.content && data.content.replace(/<[^>]+>/g, '');
            var contentLower = content && content.toLowerCase();
            // 删除重复的 /
            var postURL = data.url && decodeURI(data.url).replace(/\/{2,}/g, '/');
            // 标题中匹配到的关键词
            var titleHitSlice = [];
            // 内容中匹配到的关键词
            var contentHitSlice = [];

            keywords.forEach(function (keyword) {
              /**
              * 获取匹配的关键词的索引
              * @param {String} keyword 要匹配的关键字
              * @param {String} text 原文字
              * @param {Boolean} caseSensitive 是否区分大小写
              * @param {Number} weight 匹配对象的权重。权重大的优先显示
              * @return {Array}
              */
              function getIndexByword (word, text, caseSensitive, weight) {
                if (!word || !text) {
                  return [];
                };

                var startIndex = 0; // 每次匹配的开始索引
                var index = -1;     // 匹配到的索引值
                var result = [];    // 匹配结果

                if (!caseSensitive) {
                  word = word.toLowerCase();
                  text = text.toLowerCase();
                }

                while((index = text.indexOf(word, startIndex)) !== -1) {
                  var hasMatch = false;
                  // 索引位置相同的关键词，保留长度较长的
                  titleHitSlice.forEach(function (hit) {
                    if (hit.index === index && hit.word.length < word.length) {
                      hit.word = word;
                      hasMatch = true;
                    }
                  });
                  startIndex = index + word.length;
                  !hasMatch && result.push({ index: index, word: word, weight: weight });
                }
                return result;
              }
              titleHitSlice = titleHitSlice.concat(getIndexByword(keyword, titleLower, false, WEIGHT.title));
              contentHitSlice = contentHitSlice.concat(getIndexByword(keyword, contentLower, false, WEIGHT.content));
            });

            var hitTitle = titleHitSlice.length;
            var hitContent = contentHitSlice.length;

            if (hitTitle > 0 || hitContent > 0) {
              isMatch = true;
            }
            if (isMatch) {
              ;[titleHitSlice, contentHitSlice].forEach(function (hit) {
                // 按照匹配文字的索引的递增顺序排序
                hit.sort(function (left, right) {
                  return left.index - right.index;
                });
              });
              /**
              * 给文本中匹配到的关键词添加标记，从而进行高亮显示
              * @param {String} text 原文本
              * @param {Array} hitSlice 匹配项的索引信息
              * @param {Number} start 开始索引
              * @param {Number} end 结束索引
              * @return {String}
              */
              function highlightKeyword (text, hitSlice, start, end) {
                if (!text || !hitSlice || !hitSlice.length) {
                  return;
                }

                var result = '';
                var startIndex = start;
                var endIndex = end;
                hitSlice.forEach(function (hit) {
                  if (hit.index < startIndex) {
                    return;
                  }

                  var hitWordEnd = hit.index + hit.word.length;
                  result += text.slice(startIndex, hit.index);
                  result += '<b>' + text.slice(hit.index, hitWordEnd) + '</b>';
                  startIndex = hitWordEnd;
                });
                result += text.slice(startIndex, endIndex);
                return result;
              }

              var postData = {};
              // 文章总的搜索权重
              var postWeight = titleHitSlice.length * WEIGHT.title + contentHitSlice.length * WEIGHT.content;
              // 标记匹配关键词后的标题
              var postTitle = highlightKeyword(title, titleHitSlice, 0, title.length) || title;
              // 标记匹配关键词后的内容
              var postContent;
              // 显示内容的长度
              var SHOW_WORD_LENGTH = 200;
              // 命中关键词前的字符显示长度
              var SHOW_WORD_FRONT_LENGTH = 20;
              var SHOW_WORD_END_LENGTH = SHOW_WORD_LENGTH - SHOW_WORD_FRONT_LENGTH;

              // 截取匹配的第一个字符，前后共 200 个字符来显示
              if (contentHitSlice.length > 0) {
                var firstIndex = contentHitSlice[0].index;
                var start = firstIndex > SHOW_WORD_FRONT_LENGTH ? firstIndex - SHOW_WORD_FRONT_LENGTH : 0;
                var end = firstIndex + SHOW_WORD_END_LENGTH;
                postContent = highlightKeyword(content, contentHitSlice, start, end);
              } else { // 未匹配到内容，直接截取前 200 个字符来显示
                postContent = content.slice(0, SHOW_WORD_LENGTH);
              }
              postData.title = postTitle;
              postData.content = postContent;
              postData.url = postURL;
              postData.weight = postWeight;
              matchPosts.push(postData);
            }
          });
        }

        var resultInnerHtml = '';
        if (matchPosts.length) {
          // 按权重递增的顺序排序，使权重大的优先显示
          matchPosts.sort(function (left, right) {
            return right.weight - left.weight;
          });
          resultInnerHtml += '<ul>';
          matchPosts.forEach(function (post) {
            resultInnerHtml += '<li><a class="search-results-title" href="' + post.url + '">';
            resultInnerHtml += post.title;
            resultInnerHtml += '</a><div class="search-results-content">';
            resultInnerHtml += post.content;
            resultInnerHtml += '</div></li>';
          });
          resultInnerHtml += '</ul>';
        } else {
          resultInnerHtml += '<div class="search-results-none"><i class="far fa-meh"></i></div>';
        }
        $result.html(resultInnerHtml);
      };
      $input.on('input', searchPost);
      $input.on('keyup', function (e) {
        if (e.keyCode === Stun.utils.codeToKeyCode('Enter')) {
          searchPost();
        }
      });
    }
  });
}

function closeSearch () {
  $('body').css({ overflow: 'auto' });
  $('.search-popup').css({ display: 'none' });
  $('.search-mask').css({ display: 'none' });
}

window.addEventListener('DOMContentLoaded', function () {
  Stun.utils.pjaxReloadLocalSearch = function () {
    $('.header-nav-search').on('click', function (e) {
      e.stopPropagation();
      $('body').css('overflow', 'hidden');
      $('.search-popup')
        .velocity('stop')
        .velocity('transition.expandIn', {
          duration: 300,
          complete: function () {
            $('.search-popup input').focus();
          }
        });
      $('.search-mask')
        .velocity('stop')
        .velocity('transition.fadeIn', {
          duration: 300
        });

      initSearch();
    });
    $('.search-mask, .search-close').on('click', function () {
      closeSearch();
    });
    $(document).on('keydown', function (e) {
      // Escape <=> 27
      if (e.keyCode === Stun.utils.codeToKeyCode('Escape')) {
        closeSearch();
      }
    });
  };

  Stun.utils.pjaxReloadLocalSearch();
}, false);

function safeOpenUrl(url) {
  var newTab = window.open();
  newTab.opener = null;
  newTab.location = url;
}

function extSearch(engine) {
  var engines = {
    google: 'https://www.google.com/search?q=',
    bing: 'https://cn.bing.com/search?q=',
    baidu: 'https://www.baidu.com/s?ie=UTF-8&wd=',
  };
  var host = window.location.host;
  var query = $('.search-input input').val().toLowerCase().trim();
  var uri = engines[engine] + query + ' site:' + host;

  if (query) {
    safeOpenUrl(uri);
  } else {
    Stun.utils.popAlert('warning', '请输入字符');
  }
}

var assistSearchList = window.CONFIG.assistSearch;

if (Array.isArray(assistSearchList)) {
  assistSearchList.forEach(function (name) {
    document.querySelector('.search-btns-item--' + name).addEventListener('click', function () {
      extSearch(name);
    }, false);
  });
}</script><script src="/blog/js/utils.js?v=2.6.2"></script><script src="/blog/js/stun-boot.js?v=2.6.2"></script><script src="/blog/js/scroll.js?v=2.6.2"></script><script src="/blog/js/header.js?v=2.6.2"></script><script src="/blog/js/sidebar.js?v=2.6.2"></script><script type="application/json" src="/blog/search.json"></script></body></html>